HashMap |
k=v的排列顺序 |
Hashtable |
key降序排列(从大到小数字排列) |
TreeMap |
key升序排列(从小到大数字排列) |
LinkedHashMap |
按照k=v放入的顺序来排列 |
1 import java.util.Map;
2 public class JavaMapTest {
3 static public void main(String[] arg){
4 Map<String, String> hm = new HashMap();
5 Map<String, String> ht = new Hashtable();
6 Map<String, String> tm = new TreeMap();
7 Map<String, String> lhm = new LinkedHashMap();
8 setData(hm);
9 setData(ht);
10 setData(tm);
11 setData(lhm);
12 System.out.println("HashMap: " + hm);
13 System.out.println("Hashtable: " + ht);
14 System.out.println("TreeMap: " + tm);
15 System.out.println("LinkedHashMap: " + lhm);
16 }
17 static private void setData(Map<String,String> m){
18 m.put("2", "200");
19 m.put("3", "300");
20 m.put("1", "100");
21 m.put("5", "a");
22 m.put("6", "b");
23 m.put("4", "c");
24 }
25 }
执行结果如下:
HashMap: {3=300, 5=a, 2=200, 4=c, 6=b, 1=100}
Hashtable: {6=b, 5=a, 4=c, 3=300, 2=200, 1=100}
TreeMap: {1=100, 2=200, 3=300, 4=c, 5=a, 6=b}
LinkedHashMap: {2=200, 3=300, 1=100, 5=a, 6=b, 4=c}