Saturday, 1 September 2018

Hashmap and IdentityHashMap Example.........

package govind; import java.util.HashMap; import java.util.IdentityHashMap; public class Test { public static void main(String[] args) { Integer i1=new Integer(10); Integer i2=new Integer(10); System.out.println(i1.equals(i2));//true System.out.println(i1==i2);//false HashMap m=new HashMap();//internally equals method use m.put(i1, "govind"); m.put(i2, "khan"); System.out.println(m); IdentityHashMap i=new IdentityHashMap<>();//internally == method use i.put(i1, "govind"); i.put(i2, "khan"); System.out.println(i); } }

//o/p:-------------------------------------
true
false
{10=khan}
{10=govind, 10=khan}

=================================================================================
package govind; import java.util.HashMap; import java.util.IdentityHashMap; public class IdentityHashmapDemo { public static void main(String[] args) { HashMap hmap=new HashMap<>(); IdentityHashMap imap=new IdentityHashMap<>(); hmap.put(new String("Mobile"), "samsung"); hmap.put(new String("Laptop"), "Dell"); hmap.put(new String("Mobile"), "samsung"); System.out.println(hmap); imap.put(new String("Mobile"), "samsung"); imap.put(new String("Laptop"), "Dell"); imap.put(new String("Mobile"), "samsung"); System.out.println(imap); } }


o/p..........................................................................
{Laptop=Dell, Mobile=samsung} {Mobile=samsung, Mobile=samsung, Laptop=Dell}

No comments:

Post a Comment