java 根据对象的成员变量从值对 HashMap 进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15541777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sort HashMap basis on Object's member variable from value
提问by Navnath
Have one class
上一节课
class Employee {
int id;
String name;
}
and one map which contains this object in value
以及一张包含该对象价值的地图
Map<Integer, Employee> map = new HashMap<Integer, Employee>();
Now I want to sort above map
one the basis of Employee's name
. Means when I iterate this map using Map.Entry
, Employee
objects must retrive alphabetically.
现在,我要排序上面map
一个基础Employee's name
。意味着当我使用 迭代此地图时Map.Entry
,Employee
对象必须按字母顺序检索。
Thanks in advance
提前致谢
采纳答案by Navnath
Micha?l suggested one link which is Sort a Map<Key, Value> by values (Java). I did some change in it. and it works for me
Micha?l 建议了一个链接,它是Sort a Map<Key, Value> by values (Java)。我做了一些改变。它对我有用
class ValueComparator implements Comparator<Integer> {
Map<Integer, Employee> base;
public ValueComparator(Map<Integer, Employee> base) {
this.base = base;
}
// Note: this comparator imposes orderings that are inconsistent with equals.
public int compare(Integer a, Integer b) {
return ((Employee)base.get(a)).compareTo(base.get(b));
}
}
class Employee implements Comparable {
public String name;
public int id;
Employee(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public int compareTo(Object obj) {
return this.name.compareTo(((Employee)obj).name);
}
public String toString() {
return name;
}
}
For solution please refere above mension link too.
有关解决方案,请参阅上面的链接。
Thanks for all who have reply.
感谢所有回复的人。
回答by anubhava
Use a TreeMapwith a custom Comparatorusing this constructor:
使用带有自定义Comparator的TreeMap使用此构造函数:
http://docs.oracle.com/javase/6/docs/api/java/util/TreeMap.html#TreeMap(java.util.Comparator)
http://docs.oracle.com/javase/6/docs/api/java/util/TreeMap.html#TreeMap(java.util.Comparator)
回答by dcernahoschi
You can't sort a HashMap
, but you can sort its entries obtained with entrySet()
.
您不能对 进行排序HashMap
,但可以对通过 获得的条目进行排序entrySet()
。
public class MapSort {
private static class Employee {
public String name;
public Employee(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
public static void main(String[] args) {
Map<Integer, Employee> map = new HashMap<Integer, Employee>();
map.put(1, new MapSort.Employee("x"));
map.put(2, new MapSort.Employee("a"));
map.put(3, new MapSort.Employee("f"));
List<Map.Entry<Integer, Employee>> entryList = new ArrayList<Map.Entry<Integer, Employee>>(map.entrySet());
Collections.sort(
entryList, new Comparator<Map.Entry<Integer, Employee>>() {
@Override
public int compare(Map.Entry<Integer, Employee> integerEmployeeEntry,
Map.Entry<Integer, Employee> integerEmployeeEntry2) {
return integerEmployeeEntry.getValue().name
.compareTo(integerEmployeeEntry2.getValue().name);
}
}
);
System.out.println(entryList);
}
}
After sorting you can put back your entries in a map that supports ordering, for example LinkedHashMap.
排序后,您可以将条目放回支持排序的映射中,例如LinkedHashMap。
It depends on your use case: if you need to keep the map always sorted it's simpler to employ a TreeMap
that comes with an additional overhead. If you need just an one time sorting, you can use a HashMap
with the above code.
这取决于您的用例:如果您需要始终对地图进行排序,那么使用TreeMap
带有额外开销的a会更简单。如果你只需要一次排序,你可以使用HashMap
上面的代码。