zoukankan      html  css  js  c++  java
  • [Java数据结构]LinkedHashMap,TreeMap

    HashMap不能记住插入时的顺序,但LinkedHashMap可以做到这一点。

    例程:

            Map<Integer,String> empMap=new LinkedHashMap<Integer,String>();
            
            empMap.put(4, "Doglas");
            empMap.put(1005, "4232423432");
            empMap.put(1, "Andy");
            empMap.put(3, "Cindy");
            empMap.put(5, "Edin");
            empMap.put(2, "Bill");
            
            for(int key:empMap.keySet()) {
                System.out.println(key+","+empMap.get(key));
            }

    输出:

    4,Doglas
    1005,4232423432
    1,Andy
    3,Cindy
    5,Edin
    2,Bill

    而TreeMap则能做到按Key排序。

    例程:

            Map<Integer,String> empMap=new TreeMap<Integer,String>();
            
            empMap.put(4, "Doglas");
            empMap.put(1005, "4232423432");
            empMap.put(1, "Andy");
            empMap.put(3, "Cindy");
            empMap.put(5, "Edin");
            empMap.put(2, "Bill");
            
            for(int key:empMap.keySet()) {
                System.out.println(key+","+empMap.get(key));
            }

    输出:

    1,Andy
    2,Bill
    3,Cindy
    4,Doglas
    5,Edin
    1005,4232423432

    --END-- 2019-12-24 10:01

  • 相关阅读:
    日报10.29
    日报10.28
    日报10.27
    周进度总结-6
    日报10.25
    日报10.23
    日报10.22
    日报10.21
    日报10.18
    STL bitset
  • 原文地址:https://www.cnblogs.com/heyang78/p/12089590.html
Copyright © 2011-2022 走看看