java --Using the Map Interface

1.HashMap Class

Sample code:

 1 package self;
 2 
 3 import java.util.HashMap;
 4 
 5 public class HashMapDemo {
 6     public static void main(String args[]) {
 7         HashMap<String,Integer> obj=new HashMap<String,Integer>();
 8         Integer iobj1=new Integer (11);
 9         Integer iobj2=new Integer (22);
10         Integer iobj3=new Integer (33);
11         Integer iobj4=new Integer (44);
12         
13         System.out.println("Size of HashMap is :"+obj.size());
14         obj.put("L1", iobj1);
15         obj.put("L2",iobj2);
16         obj.put("L3",iobj3);
17         obj.put("L4",iobj4);
18         obj.put("L5", iobj1);
19         
20         System.out.println("\nHashMap after adding the objects:"+obj);
21         System.out.println("Size of HashMap after adding objects:"+obj.size());
22         obj.remove("L3");
23         obj.remove("L5");
24         
25         System.out.println("\nHashMap after removing the objects:"+obj);
26         System.out.println("Size of the HashMap after removing objects: "+obj.size());
27     }
28 }
View Code

Test Results:

 

2.TreeMap Class

Sample code:

 1 package self;
 2 
 3 import java.util.TreeMap;
 4 
 5 public class TreeMapDemo {
 6     public static void main(String args[]) {
 7         TreeMap<Integer,String> obj=new TreeMap<Integer,String>();
 8     
 9         String sobj1=new String("Value 1");
10         String sobj2=new String("Value 2");
11         String sobj3=new String("Value 3");
12         String sobj4=new String("Value 4");
13         
14         
15         System.out.println("Size of TreeMap is :"+obj.size());
16         
17         obj.put(101, sobj1);
18         obj.put(102,sobj2);
19         obj.put(103,sobj3);
20         obj.put(104,sobj4);
21         obj.put(105, sobj1);
22         
23         System.out.println("\nTreeMap after adding the objects:"+obj);
24         System.out.println("Size of TreeMap after adding objects:"+obj.size());
25         obj.remove(103);
26         obj.remove(105);
27         
28         System.out.println("\nTreeMap after removing the objects:"+obj);
29         System.out.println("Size of the TreeMap after removing objects: "+obj.size());
30     }
31 }
View Code

Test Results:

 

 

3.HashMap Class

Sample code:

 

 1 package self;
 2 
 3 import java.util.Hashtable;
 4 
 5 public class HashtableDemo {
 6     public static void main(String args[]) {
 7         Hashtable<Integer,Double> obj=new Hashtable<Integer,Double>();
 8     
 9         Double dobj1=new Double(55.6);
10         Double dobj2=new Double(34.6);
11         Double dobj3=new Double(98.6);
12         Double dobj4=new Double(12.5);
13         
14         
15         System.out.println("Size of Hashtable is :"+obj.size());
16         
17         obj.put(55, dobj1);
18         obj.put(60,dobj2);
19         obj.put(65,dobj3);
20         obj.put(70,dobj4);
21         obj.put(75, dobj1);
22         
23         System.out.println("\nHashtable after adding the objects:"+obj);
24         System.out.println("Size of Hashtable after adding objects:"+obj.size());
25         obj.remove(65);
26         obj.remove(75);
27         
28         System.out.println("\nHashtable after removing the objects:"+obj);
29         System.out.println("Size of the Hashtable after removing objects: "+obj.size());
30     }
31 }
View Code

Test Results:

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325344913&siteId=291194637