Android中实现HashMap排序的方法

2019/7/7 20:13:33

本文主要是介绍Android中实现HashMap排序的方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

HashMap排序是数据结构与算法中常见的一种排序算法。本文即以Android平台为例来实现该算法。

具体代码如下:

public static void main(String[] args) {
 Map<String, Integer> map = new HashMap<String, Integer>();

 map.put("lisi", 5); 
 map.put("lisi1", 1); 
 map.put("lisi2", 3); 
 map.put("lisi3", 9); 

 List<Map.Entry<String, Integer>> infoIds = new ArrayList<Map.Entry<String, Integer>>(
  map.entrySet());
 System.out.println("--------------排序前--------------");
 for (int i = 0; i < infoIds.size(); i++) {
 String id = infoIds.get(i).toString();
 System.out.println(id);
 }
 // 排序
 Collections.sort(infoIds, new Comparator<Map.Entry<String, Integer>>() {
 public int compare(Map.Entry<String, Integer> o1,
  Map.Entry<String, Integer> o2) {
  return ( o1.getValue()-o2.getValue());
 }
 });
 System.out.println("--------------排序后--------------");
 for (int i = 0; i < infoIds.size(); i++) {
 Entry<String,Integer> ent=infoIds.get(i);
 System.out.println(ent.getKey()+"="+ent.getValue());
 }
}

希望本文所述的HashMap排序算法能对大家的算法学习有所帮助。



这篇关于Android中实现HashMap排序的方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程