Drop Down

Tuesday, March 5, 2019

Star Pattern 3

package interview;
/*

*
**
***
****
*****
****** 
*****
****
***
**
*

**/

public class StarPattern3
{

public static void main(String[] args)
{
  for(int i = 1 ;i<6;i++)
  {
  for(int j=0;j<i;j++)
  {
  System.out.print("*");
  }
  System.out.println();
  }
  //-----------
  for(int i = 6 ;i>0;i--)
  {
  for(int j=0;j<i;j++)
  {
  System.out.print("*");
  }
  System.out.println();
  }
 
}

}

Star Pattern 2

package interview;
/*

****** 
*****
****
***
**
*

*/
public class StarPattern2
{
public static void main(String[] args)
{
for(int i =1;i<=6;i++)
{
for(int j=6;j>=i ;j--)
{
System.out.print("*");
}
System.out.println();
}
}

}

Start Pattern 1

package interview;
/*

*
**
***
****
*****

*/
public class StarPattern1 {

public static void main(String[] args)
{
for(int i =1;i<=5;i++)
{
for(int j=1;j<=i ;j++)
{
System.out.print("*");
}
System.out.println();

}
 

}

}

SortMapByValue_TreeMap

package comparator.Emp;

import java.util.*;

public class SortMapByValue_TreeMap
{
public static void main(String[] args)
{
Map <String,String> unsortedmap = new HashMap<>();
unsortedmap.put("one", "one");
unsortedmap.put("tree", "tree");
unsortedmap.put("mount", "mount");
unsortedmap.put("bird", "bird");
unsortedmap.put("house", "house");
System.out.println("***************Unsorted Map**************");
printmap(unsortedmap);
System.out.println("************After sorting Map*************");
        Map<String, String> sortedMap = sortMap(unsortedmap);
        printmap(sortedMap);


}

private static Map<String, String> sortMap(Map<String, String> unsortedmap)
{
// copy unsortedmap to TreeMap
TreeMap <String,String> tm = new TreeMap<>(new SortMapComparator());
for(Map.Entry<String,String> entry : unsortedmap.entrySet())
{
tm.put(entry.getKey(), entry.getValue());
}
return tm;
}

private static void printmap(Map<String, String> unsortedmap)
{
  for(Map.Entry<String,String> entry  : unsortedmap.entrySet())
  {
  System.out.println(entry.getKey()+" : "+entry.getValue());
  }

}

}
=============================================================
package comparator.Emp;

import java.util.Comparator;

public class SortMapComparator implements Comparator <String>
{

@Override
public int compare(String s1 , String s2)
{
    return s1.compareTo(s2);
}


}
==============================================================

Concurrent Modification Exception

package concurrent_Collection;
/*One Thread iterating Collection Object and other thread trying to modify it...
 This leads to Concurrent Modification Exception
java.util.ConcurrentModificationException*/

import java.util.*;

public class ConcurrentModificationException extends Thread
{
static ArrayList list = new ArrayList();
public void run()
{
try
{
  Thread.sleep(700);
}
catch(Exception e)
{
System.out.println(e.toString());
}
System.out.println("Child Thread adding ---> 10");
list.add(10);
}

public static void main(String[] args) throws Exception
{
  list.add(59);
  list.add(788);
  list.add(45);
  list.add(88);
  ConcurrentModificationException thread = new ConcurrentModificationException();
  thread.start();
  Iterator itr = list.iterator();
  while(itr.hasNext())
  {
  System.out.println(itr.next());
  Thread.sleep(500);
  }

}

}

Sort HashMap according to Value

package test;

import java.util.*;

//map ---list--sort--map(display)
public class SortMap_Value
{
public static void main(String[] args)
{
Map<String,Integer> map = new HashMap<>();
map.put("A", 78);
map.put("B", 55);
map.put("C", 807);
map.put("D", 34);
map.put("E", 89);
map.put("F", 700);

//List<Map.Entry<String, Integer>> list = new LinkedList<>(map.entrySet());
List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet());
Collections.sort(list, new MyComparator2());

Map<String,Integer> map2 = new LinkedHashMap<>();
for(Map.Entry<String,Integer> entry : list)
{
map2.put(entry.getKey(), entry.getValue());
}
for(Map.Entry<String,Integer> entry : map2.entrySet())
{
System.out.println(entry.getKey()+" : "+entry.getValue());
}


}

}
========================================
package test;

import java.util.Comparator;import java.util.Map;
import java.util.Map.Entry;


public class MyComparator2 implements Comparator<Map.Entry<String,Integer>>
{

@Override
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2)
{

//return o1.getValue().compareTo(o2.getValue());
return o2.getValue().compareTo(o1.getValue());
}

}
=========================================


Collection_Links

Java 8 Notes Pics