Android 如何使用键作为字符串和值作为对象对哈希映射进行排序
我有一个HashMap,字符串作为键,对象作为值。如何在对象中按名字字段对HashMap排序。下面是实际的HashMap表示法Android 如何使用键作为字符串和值作为对象对哈希映射进行排序,android,collections,hashmap,Android,Collections,Hashmap,我有一个HashMap,字符串作为键,对象作为值。如何在对象中按名字字段对HashMap排序。下面是实际的HashMap表示法HashMap attendanceDataMap=newhashmap() HashMap的键将是AttendanceData对象的id 上课的人看起来像 public class AttendanceData{ private TakenBy takenBy; private String id; private String user_name; priva
HashMap attendanceDataMap=newhashmap()代码>
HashMap的键将是AttendanceData对象的id
上课的人看起来像
public class AttendanceData{
private TakenBy takenBy;
private String id;
private String user_name;
private String first_name;
private String description;
private String last_name;
public AttendanceData(String id, String firstName, String lastName, String takenBy, String description, String userName)
{
this.id=id;
this.first_name=firstName;
this.last_name=lastName;
this.takenBy=takenBy;
this.description=description;
this.user_name=userName;
}
我认为比较器可能会有所帮助,粘贴下面的示例,请注意这不是一个具体的答案,您必须根据需要修改它:
public class HMapSortingByvalues {
public static void main(String[] args) {
HashMap<Integer, String> hmap = new HashMap<Integer, String>();
hmap.put(5, "A");
hmap.put(11, "C");
hmap.put(4, "Z");
hmap.put(77, "Y");
hmap.put(9, "P");
hmap.put(66, "Q");
hmap.put(0, "R");
System.out.println("Before Sorting:");
Set set = hmap.entrySet();
Iterator iterator = set.iterator();
while(iterator.hasNext()) {
Map.Entry me = (Map.Entry)iterator.next();
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue());
}
Map<Integer, String> map = sortByValues(hmap);
System.out.println("After Sorting:");
Set set2 = map.entrySet();
Iterator iterator2 = set2.iterator();
while(iterator2.hasNext()) {
Map.Entry me2 = (Map.Entry)iterator2.next();
System.out.print(me2.getKey() + ": ");
System.out.println(me2.getValue());
}
}
private static HashMap sortByValues(HashMap map) {
List list = new LinkedList(map.entrySet());
// Defined Custom Comparator here
Collections.sort(list, new Comparator() {
public int compare(Object o1, Object o2) {
return ((Comparable) ((Map.Entry) (o1)).getValue())
.compareTo(((Map.Entry) (o2)).getValue());
}
});
// Here I am copying the sorted list in HashMap
// using LinkedHashMap to preserve the insertion order
HashMap sortedHashMap = new LinkedHashMap();
for (Iterator it = list.iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
sortedHashMap.put(entry.getKey(), entry.getValue());
}
return sortedHashMap;
}
}
公共类HMapSortingByvalues{
公共静态void main(字符串[]args){
HashMap hmap=新的HashMap();
hmap.put(5,“A”);
hmap.put(11,“C”);
hmap.put(4,“Z”);
hmap.put(77,“Y”);
hmap.put(9,“P”);
hmap.put(66,“Q”);
hmap.put(0,“R”);
System.out.println(“排序前:”);
Set=hmap.entrySet();
迭代器迭代器=set.Iterator();
while(iterator.hasNext()){
Map.Entry me=(Map.Entry)迭代器.next();
System.out.print(me.getKey()+“:”);
System.out.println(me.getValue());
}
Map Map=排序值(hmap);
System.out.println(“排序后:”);
Set set2=map.entrySet();
迭代器迭代器2=set2.Iterator();
while(iterator2.hasNext()){
Map.Entry me2=(Map.Entry)迭代器2.next();
System.out.print(me2.getKey()+“:”);
System.out.println(me2.getValue());
}
}
私有静态HashMap SortByValue(HashMap映射){
List=新的LinkedList(map.entrySet());
//这里定义了自定义比较器
Collections.sort(list,newcomparator(){
公共整数比较(对象o1、对象o2){
return((可比)((Map.Entry)(o1)).getValue())
.compareTo(((Map.Entry)(o2)).getValue();
}
});
//在这里,我复制HashMap中的排序列表
//使用LinkedHashMap保留插入顺序
HashMap sortedHashMap=新建LinkedHashMap();
for(Iterator it=list.Iterator();it.hasNext();){
Map.Entry=(Map.Entry)it.next();
sortedHashMap.put(entry.getKey(),entry.getValue());
}
返回SHMAP;
}
}
HashMap不支持排序。当然,您可以对值进行排序。实施类似的措施,例如:
List list = new ArrayList(Map.entrySet());
Collections.sort(list, new Comparator() {
@Override
public int compare(Entry e1, Entry e2) {
return e1.getValue().compareTo(e2.getValue());
}
});
此外,您还可以使用SortedMap(TreeMap-eg)。但它只支持按键排序。尽管您可以尝试使用此比较器:
class ValueComparator implements Comparator {
Map base;
public ValueComparator(Map base) {
this.base = base;
}
public int compare(Object a, Object b) {
if((Double)base.get(a) < (Double)base.get(b)) {
return 1;
} else if((Double)base.get(a) == (Double)base.get(b)) {
return 0;
} else {
return -1;
}
}
}
类ValueComparator实现Comparator{
地图库;
公共价值比较器(地图库){
this.base=base;
}
公共整数比较(对象a、对象b){
如果((双基)获取(a)<(双基)获取(b)){
返回1;
}else如果((双)base.get(a)==(双)base.get(b)){
返回0;
}否则{
返回-1;
}
}
}
比较器在这里会很有用。实际上,我想按名字字段对HashMap进行排序,该字段将位于HashMap包含的AttendanceData对象内。您应该使用Comperator。也许这个问题对你有帮助。
class ValueComparator implements Comparator {
Map base;
public ValueComparator(Map base) {
this.base = base;
}
public int compare(Object a, Object b) {
if((Double)base.get(a) < (Double)base.get(b)) {
return 1;
} else if((Double)base.get(a) == (Double)base.get(b)) {
return 0;
} else {
return -1;
}
}
}