zoukankan      html  css  js  c++  java
  • 如何对一个不断更新的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());
      }
    }
    
    
    
     
  • 相关阅读:
    09.Java数据算法
    08.Java反射问题
    07.Java类加载问题
    占位
    占位
    占位
    占位
    HTML与CSS粗浅摘要
    JavaScript(4):模态对话框、隐藏二级菜单及多选框的实现
    JavaScript(3):DOM选择器
  • 原文地址:https://www.cnblogs.com/byrhuangqiang/p/3654094.html
Copyright © 2011-2022 走看看