我有一个 HashMap<Object, Student>
,其中 Object 是学生的 ID,而 Student 是来自 Student 类的对象。
我该如何通过学生的姓名 student->getName()
对 HashMap 进行重新排序?
我有一个 HashMap<Object, Student>
,其中 Object 是学生的 ID,而 Student 是来自 Student 类的对象。
我该如何通过学生的姓名 student->getName()
对 HashMap 进行重新排序?
虽然无法对HashMap进行排序,但是可以通过一些方法达到同样的效果。我使用在Javarevisited博客上发布的优秀代码,按照Integer的降序值来排序我的HashMap <String, Integer>对象。相同的原理适用于HashMap<String,String>对象。
/*
* Java method to sort Map in Java by value e.g. HashMap or Hashtable
* throw NullPointerException if Map contains null values
* It also sort values even if they are duplicates
*/
public static <K extends Comparable,V extends Comparable> Map<K,V> sortByValues(Map<K,V> map){
List<Map.Entry<K,V>> entries = new LinkedList<Map.Entry<K,V>>(map.entrySet());
Collections.sort(entries, new Comparator<Map.Entry<K,V>>() {
@Override
public int compare(Entry<K, V> o1, Entry<K, V> o2) {
return o1.getValue().compareTo(o2.getValue());
// to compare alphabetically case insensitive return this instead
// o1.getValue().toString().compareToIgnoreCase(o2.getValue().toString());
}
});
//LinkedHashMap will keep the keys in the order they are inserted
//which is currently sorted on natural ordering
Map<K,V> sortedMap = new LinkedHashMap<K,V>();
for(Map.Entry<K,V> entry: entries){
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}
调用此方法,我使用:
Map<String, Integer> sorted = sortByValues(myOriginalHashMapObject);
本文介绍如何在Java中通过键和值对HashMap进行排序。
地图无法按值排序。但您可以这样做:
Collection<Student> students = map.values();
Collection.sort(new ArrayList<Student>(students)), new Comparator<Student>() {
public int compare(Student s1, Student s2) {
return s1.getName().compareTo(s2.getName());
}
});
HashMap 无法按其值进行排序。Map 是为基于键的常数时间查找而设计的,因此按值排序不是必要的。如果您需要按名称排序,建议使用 SortedSet
并创建一个按名称排序的比较器。
class StudentComparator implements Comparator<Student> {
int compare(Student s1, Student s2) {
return s1.getName().compareTo(s2.getName());
}
}
我肯定会使用一个新的类来存储键和对象。
然后,您可以将Map的每个元素以此类的形式放入ArrayList中,最后使用比较器对ArrayList进行排序,然后简单地构建一个新的Map。 代码将如下所示:
Map<Object, Student> valueMap = new LinkedHashMap<String, String>();
List<Student> pairValueList = new ArrayList<PairValue>();
PairValue p;
for (Map.Entry<Object, Student> entry : map.entrySet()) {
Object key = entry.getKey();
Student value = entry.getValue();
p = new PairValue(key, value);
pairValueList.add(p);
}
Collections.sort(pairValueList, new Comparator<PairValue>() {
@Override
public int compare(PairValue c1, PairValue c2) {
return c1.getLabel().compareTo(c2.getLabel());
}
});
for (PairValue pv : pairValueList) {
valueMap.put(pv.getValue(), pv.getStudent());
}
PairValue类
class PairValue {
private Object value;
private Student student;
public PairValue(Object value, String student) {
this.value = value;
this.student= student;
}
public String getValue() {
return value;
}
public String getStudent() {
return student;
}
}
这是我解决过类似问题的方式。请注意,返回的 map 实现需要是一个 LinkedHashMap。