TreeMap,HashMap,LinkedHashMap区别
TreeMap,HashMap,LinkedHashMap之间的区别和TreeSet,HashSet,LinkedHashSet之间的区别相似。
TreeMap:内部排序,内部使用了红黑树排序
HashMap:无序。
LinkedHashMap:顺序存取,内部是单向链表存储数据
1 Map<String,String> map1=new LinkedHashMap<String,String>(); 2 Map<String,String> map2 = new TreeMap<>(); 3 Map<String,String> map3 = new HashMap<>(); 4 map1.put("d","111"); 5 map1.put("c","222"); 6 map1.put("a","333"); 7 8 map2.put("d","111"); 9 map2.put("c","222"); 10 map2.put("a","333"); 11 12 map3.put("d","111"); 13 map3.put("c","222"); 14 map3.put("a","333"); 15 16 17 System.out.print(map1.toString()); 18 System.out.print("\n"); 19 System.out.print(map2.toString()); 20 System.out.print("\n"); 21 System.out.print(map3.toString()); 22
输出:
{d=111, c=222, a=333}
{a=333, c=222, d=111}
{a=333, c=222, d=111}