Java部分集合有序性记录

本文深入探讨Java集合框架中各类集合的特性与使用场景,包括HashMap、Hashtable、TreeMap、LinkedHashMap、ConcurrentSkipListMap、ConcurrentHashMap、ArrayList、LinkedList等,通过代码演示不同集合在元素插入、遍历及排序等方面的差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1表格结果

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

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值大小修改一下,就会发现再次编译出来的结果是完全没有秩序了。
  • 以上仅为我的个人验证,如有错误,欢迎指出!谢谢

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值