如何对一个不断更新的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());
  }
}


 
原文地址:https://www.cnblogs.com/byrhuangqiang/p/3654094.html