• 如何对一个不断更新的HashMap进行排序


    如何对一个不断更新的HashMap进行排序?

    解答:等到HashMap更新稳定后,用ArrayList包装进行排序。或者自己写一个可以类似HashMap的有序Map,每次更新的时候都进行排序,构建自己的put方法,并且可以排序。
    大屏项目中采用的排序:
        Map<String, Integer> itemCount = new HashMap<String, Integer>();
        Comp cmp = new Comp();
        List<Map.Entry<String, Integer>> itemSort;
        itemSort = new ArrayList<Map.Entry<String, Integer>>( itemCount.entrySet()); 
        Collections.sort(itemSort, cmp);

    //Comp.java
    import java.util.Comparator;
    import java.util.Map;
    import java.util.Map.Entry;
    
    public class Comp implements Comparator<Map.Entry<String, Integer>> {
      @Override
      public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
          return (o2.getValue() - o1.getValue());
      }
    }
    
    
    
     
  • 相关阅读:
    2021-4-20 日报博客
    2021-4-19 日报博客
    2021-4-17 周报博客
    java web
    java web
    java web
    java
    java
    周末总结8
    java web
  • 原文地址:https://www.cnblogs.com/byrhuangqiang/p/3654094.html
Copyright © 2020-2023  润新知