Java_map

 1 package Test;
 2 
 3 import java.util.HashMap;
 4 import java.util.Map;
 5 
 6 public class MapTest {
 7     public static void main(String[]args){
 8         Map map1=new HashMap();
 9         Map map2=new HashMap();
10         map1.put("1","aaa1");
11         map1.put("1","bbb2");
12         map2.put("10","aaaa10");
13         map2.put("11","bbbb11");
14         
15         System.out.println("map1.get(\"1\")="+map1.get("1"));
16         System.out.println("map1.remove(\"1\")="+map1.remove("1"));
17         map1.putAll(map2);
18         map2.clear();
19         System.out.println("map1 IsEmpty?="+map1.isEmpty());
20         System.out.println("map2 IsEmpty?="+map2.isEmpty());
21         System.out.println("map中的键值对数size"+map1.size());
22         System.out.println("KeySet="+map1.keySet());
23         System.out.println("value="+map1.values());
24         System.out.println("entrySet="+map1.entrySet());
25         System.out.println("map1是否包含键:11="+map1.containsKey("11"));
26         System.out.println("map1是否包含值:aaa1="+map1.containsValue("aaa1"));
27     }
28     
29 }
 1 package Test;
 2 
 3 import java.util.HashMap;
 4 import java.util.LinkedHashMap;
 5 import java.util.Map;
 6 import java.util.TreeMap;
 7 
 8 public class MapSortExample {
 9     public static void main(String[]args){
10         Map map1=new HashMap();
11         Map map2=new LinkedHashMap();
12         for(int i=0;i<10;i++){
13             double s=Math.random()*100;
14             map1.put(new Integer((int) s),"第 "+i+"个放入的元素:"+s+"\n");
15             map2.put(new Integer((int) s),"第 "+i+"个放入的元素:"+s+"\n");
16         }
17             
18         
19         System.out.println("未排序前HashMap:"+map1);
20         System.out.println("未排序前LinkedHashMap:"+map2);
21         Map sortedMap=new TreeMap(map1);
22         System.out.println("排序后:"+sortedMap);
23         System.out.println("排序前:"+new TreeMap(map2));
24     }
25 
26 }

 

posted @ 2014-04-13 19:59  zhangyongjian  阅读(217)  评论(0编辑  收藏  举报