Java部分集合有序性记录

1表格结果

序号 集合名称 进出集合 key大小
1 HashMap 无序 无序
2 Hashtable 无序 无序
3 TreeMap 无序 从小到大
4 LinkedHashMap 先进先出 无序
5 ConcurrentSkipListMap 无序 从小到大
6 ConcurrentHashMap 无序 无序
7 ArrayList 先进先出 无序
8 LinkedList 先进先出 无序
       

2代码验证

        System.out.println("放入顺序为:a:111 c:333 b:222 d:444 ");
        System.out.println("HashMap 排序测试");
        Map map = new HashMap();
        map.put("a1", "111");
        map.put("c1", "333");
        map.put("b1", "222");
        map.put("d1", "444");
        Iterator iterator = map.keySet().iterator();
        while (iterator.hasNext()) {
            Object key = iterator.next();
            System.out.println("key:" + key + " value:" + map.get(key));
        }
        System.out.println("HashMap 进出无次序 ; key大小无次序 ");
        System.out.println("-----------------------------------------------------------");


        System.out.println("Hashtable 排序测试");
        Hashtable tab = new Hashtable();
        tab.put("a", "111");
        tab.put("c", "333");
        tab.put("b", "222");
        tab.put("d", "444");
        Iterator iterator_1 = tab.keySet().iterator();
        while (iterator_1.hasNext()) {
            Object key = iterator_1.next();
            System.out.println("key :" + key + " value:" + tab.get(key));
        }
        System.out.println("Hashtable 进出无次序 ; key大小无次序 ");
        System.out.println("-----------------------------------------------------------");

        System.out.println("TreeMap 排序测试");
        TreeMap tmp = new TreeMap();
        tmp.put("a1", "111");
        tmp.put("c2", "333");
        tmp.put("b3", "222");
        tmp.put("d4", "444");
        Iterator iterator_2 = tmp.keySet().iterator();
        while (iterator_2.hasNext()) {
            Object key = iterator_2.next();
            System.out.println("key:" + key + " value:" + tmp.get(key));
        }
        System.out.println("TreeMap 进出无次序 ; key大小从小到大 ");
        System.out.println("-----------------------------------------------------------");

        System.out.println("LinkedHashMap 排序测试");
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        linkedHashMap.put("a", "111");
        linkedHashMap.put("c", "333");
        linkedHashMap.put("b", "222");
        linkedHashMap.put("d", "444");
        Iterator iterator2 = linkedHashMap.keySet().iterator();
        while (iterator2.hasNext()) {
            Object key = iterator2.next();
            System.out.println("key:" + key + " value:" + linkedHashMap.get(key));
        }
        System.out.println("LinkedHashMap 先进先出; key大小无次序 ");
        System.out.println("-----------------------------------------------------------");
        ConcurrentSkipListMap concurrentSkipListMap = new ConcurrentSkipListMap();
        concurrentSkipListMap.put("a", "111");
        concurrentSkipListMap.put("c", "333");
        concurrentSkipListMap.put("b", "222");
        concurrentSkipListMap.put("d", "444");
        Iterator iterator3 = concurrentSkipListMap.keySet().iterator();
        while (iterator3.hasNext()) {
            Object key = iterator3.next();
            System.out.println("key:" + key + " value:" + concurrentSkipListMap.get(key));
        }
        System.out.println("ConcurrentSkipListMap 进出无次序 ; key大小从小到大  ");
        System.out.println("-----------------------------------------------------------");
        System.out.println();
        ConcurrentHashMap concurrentHashMap = new ConcurrentHashMap();
        concurrentHashMap.put("a1", "111");
        concurrentHashMap.put("c1", "333");
        concurrentHashMap.put("b1", "222");
        concurrentHashMap.put("d1", "444");
        Iterator iterator4 = concurrentHashMap.keySet().iterator();
        while (iterator4.hasNext()) {
            Object key = iterator4.next();
            System.out.println("key:" + key + " value:" + concurrentHashMap.get(key));
        }
        System.out.println("ConcurrentHashMap 进出无次序 ; key大小无次序  ");
        System.out.println("-----------------------------------------------------------");
        System.out.println();

        System.out.println("ArrayList 排序测试");
        System.out.println("放入顺序为:111 333 222 444");
        ArrayList arrayList = new ArrayList();
        arrayList.add("111");
        arrayList.add("333");
        arrayList.add("222");
        arrayList.add("444");
        System.out.println("ArrayList 先进先出;值大小无次序 ");
        for (int i = 0; i < arrayList.size(); i++) {
            System.out.println(arrayList.get(i));
        }
        System.out.println("-----------------------------------------------------------");
        System.out.println("LinkedList 排序测试");
        LinkedList linkedList = new LinkedList();
        linkedList.add("111");
        linkedList.add("333");
        linkedList.add("222");
        linkedList.add("444");
        System.out.println("LinkedList 先进先出;值大小无次序 ");
        for (int i = 0; i < arrayList.size(); i++) {
            System.out.println(arrayList.get(i));
        }
        System.out.println("-----------------------------------------------------------");

3代码结果

放入顺序为:a:111 c:333 b:222 d:444 
HashMap 排序测试
key:a1 value:111
key:d1 value:444
key:c1 value:333
key:b1 value:222
HashMap 进出无次序 ; key大小无次序 
-----------------------------------------------------------
Hashtable 排序测试
key :b value:222
key :a value:111
key :d value:444
key :c value:333
Hashtable 进出无次序 ; key大小无次序 
-----------------------------------------------------------
TreeMap 排序测试
key:a1 value:111
key:b3 value:222
key:c2 value:333
key:d4 value:444
TreeMap 进出无次序 ; key大小从小到大 
-----------------------------------------------------------
LinkedHashMap 排序测试
key:a value:111
key:c value:333
key:b value:222
key:d value:444
LinkedHashMap 先进先出; key大小无次序 
-----------------------------------------------------------
key:a value:111
key:b value:222
key:c value:333
key:d value:444
ConcurrentSkipListMap 进出无次序 ; key大小从小到大  
-----------------------------------------------------------

key:a1 value:111
key:d1 value:444
key:c1 value:333
key:b1 value:222
ConcurrentHashMap 进出无次序 ; key大小无次序  
-----------------------------------------------------------

ArrayList 排序测试
放入顺序为:111 333 222 444
ArrayList 先进先出;值大小无次序 
111
333
222
444
-----------------------------------------------------------
LinkedList 排序测试
LinkedList 先进先出;值大小无次序 
111
333
222
444
-----------------------------------------------------------

 后记:

  • 有的遍历看起来是按自然顺序排列的,有点像是红黑树的结果,但事实是这个可能和编译器的工作原理有关系,你可以在编译一次后,将Map的key值大小修改一下,就会发现再次编译出来的结果是完全没有秩序了。
  • 以上仅为我的个人验证,如有错误,欢迎指出!谢谢

 

 

你可能感兴趣的:(Java部分集合有序性记录)