本文共 3859 字,大约阅读时间需要 12 分钟。
Java中HashMap不同于Array、ArrayList和LinkedLists,它不会维持插入元素的顺序。因此对HashMap排序的掌握很重要。HashMap排序有两个步骤:
关于map的遍历方法可参考
例如下面的按照value降序排列:Collections.sort(list, new Comparator>() { public int compare(Entry ele1, Entry ele2) { return ele1.getValue().compareTo(ele2.getValue()); } });
灵活使用Comparator接口可以实现HashMap的灵活排序:包括分别对键、值的升降序排序,优先按值、其次按键排序,或者优先按键、其次按值排序;以上实现只需重写compare方法。
1、对value降序排列
public int compare(Map.Entrye1,Map.Entry e2){ return e2.getValue().compareTo(e1.getValue()); }
2、对键KEY降序排列
public int compare(Map.Entrye1,Map.Entry e2){ return e2.getKey().compareTo(e1.getKey()); }
3、优先按值value降序、其次按键key升序排序
public int compare(Map.Entrye1,Map.Entry e2){ int re = e2.getValue().compareTo(e1.getValue()); if(re!=0){ return re;} else{ return e1.getKey().compareTo(e2.getKey());} }
4、优先按键key降序、其次按值value升序排序
public int compare(Map.Entrye1,Map.Entry e2){ int re = e2.getKey().compareTo(e1.getKey()); if(re!=0){ return re;} else{ return e1.getValue().compareTo(e2.getValue());} }
例题:
题意描述:输入一个只包含小写英文字母和数字的字符串,按照不同字符统计个数由多到少输出统计结果,如果统计的个数相同,则按照ASCII码由小到大排序输出。 本题含有多组样例输入 很简单的一个思路就是先对map的值排序,然后再按照键排序。import java.util.*;public class Main{ public static void main(String[] args) { Scanner cin=new Scanner(System.in); while(cin.hasNext()){ Mapmap = new HashMap (); String string=cin.next(); for(int i=0;i > list = new ArrayList<>(); list.addAll(map.entrySet()); Collections.sort(list,new Comparator >(){ public int compare(Map.Entry e1,Map.Entry e2){ int re = e2.getValue().compareTo(e1.getValue()); if(re!=0){ return re;} else{ return e1.getKey().compareTo(e2.getKey());} } }); for(Map.Entry map1:list){ System.out.print(map1.getKey()); } System.out.println(); } }}
当然,我们也可以利用普通类实现Comparable接口来对数据进行自定义排序
import java.util.*;public class Main { public static void main(String[] args) { Scanner cin=new Scanner(System.in); while(cin.hasNext()){ Mapmap = new HashMap (); String string=cin.next(); for(int i=0;i list=new ArrayList<>(); for(Map.Entry entry:map.entrySet()){ User user=new User(); user.setKey(entry.getKey()); user.setValue(entry.getValue()); list.add(user); user=null;//释放对象 } Collections.sort(list); for (User user:list){ System.out.print(user.getKey()); } System.out.println(); } } static class User implements Comparable { private Character key; private Integer value; public User() { } @Override public int compareTo(User user) { if(user.value==this.value) return this.key.compareTo(user.key); //value相同key升序 return user.value.compareTo(this.value); //value不同则value降序 } public Character getKey() { return key; } public void setKey(Character key) { this.key = key; } public Integer getValue() { return value; } public void setValue(Integer value) { this.value = value; } }}
转载地址:http://qyncf.baihongyu.com/