Posts

TreeMap with Duplicate Keys

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 import java.util.Comparator ; import java.util.TreeMap ; public class TreeMapFlight { public static void main ( String [] bag ){ TreeMap < Integer , String > tm = new TreeMap < Integer , String >( new MyComp ()); tm . put ( 1 , "one" ); tm . put ( 1 , "null1" ); tm . put ( 1 , "null2" ); tm . put ( 1 , "null3" ); System . out . println ( tm ); } } class MyComp implements Comparator { @Override public int compare ( Object o1 , Object o2 ) { return 1 ; } } OUTPUT { 1 = one , 1 = null1 , 1 = null2 , 1 = null3 }

Condition Test and TreeMap

import java.util.Map; import java.util.TreeMap; public class ConditionTest { public static void main(String[] bag){ int n=10; if((n/2)==5){ System.out.println("10/2 = 5"); } Map<String, String> map = new TreeMap<String, String>(); map.put("1", ""); map.put("1", ""); map.put("1", ""); System.out.println("Size="+map.size()); } } OUTPUT 10/2==5 Size=1

ConcurrentExceptionFlight

import java.util.ArrayList; import java.util.Iterator; public class ConcurrentExceptionFlight { public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); /*list.set(0,"100"); it will throw java.lang.IndexOutOfBoundsException */  list.add("1"); list.add("2"); Iterator<String> iterator = list.iterator(); while (iterator.hasNext()) { iterator.next(); // without this line it will throw ConcurrentExceptionFlight iterator.remove(); // you can remove element while iterating using iterator } for(String string : list){ System.out.println(string); } } } OUTPUT

PassByValue Test

class User{ private String name; public User(String name){ this.name = name; } public String getName(){ return name; } public void setName(String name){ this.name = name; } } public class PassByValueFlight { public static void main(String[] args){ User user = new User("Raj"); System.out.println(user.getName()); updateName(user); System.out.println(user.getName()); updateNameValue(user); System.out.println(user.getName()); } public static void updateName(User user){ user = new User("dharmraj"); } public static void updateNameValue(User user){ user.setName("galaxy"); } } OUTPUT Raj Raj galaxy

TreeMap Size

import java.util.Map; import java.util.TreeMap; class Test implements Comparable{ int a; Test(int a){ this.a = a; } @Override public int compareTo(Object arg0) { return this.a - ((Test)arg0).a; } } public class TreeMapTest { public static void main(String[] args){ Test t1 =  new Test(1); Test t2 =  new Test(1); Map<Test,Integer> map = new TreeMap<>(); map.put(t1, 1); map.put(t2, 1); System.out.println("Size="+map.size()); } } OUTPUT Size=1

TreeMap Test

import java.util.Map; import java.util.TreeMap; class Test{ int a; Test(int a){ this.a = a; }   } public class TreeMapTest { public static void main(String[] args){ Test t1 =  new Test(1); Test t2 =  new Test(1); Map<Test,Integer> map = new TreeMap<>(); map.put(t1, 1); map.put(t2, 1); System.out.println(map.size()); } } OUTPUT Exception in thread "main" java.lang.ClassCastException: Test cannot be cast to java.lang.Comparable