[英]how to sort a vector of hashmaps
i have a Vector of Hashmap and the HashMap contains different data types: 我有一个Hashmap的Vector,并且HashMap包含不同的数据类型:
Vector<HashMap<String, Object>> theVector= new Vector<HashMap<String, Object>>();
theResults contains this HashMap: theResults包含以下HashMap:
HashMap<String, Object> theHashMap= new HashMap<String, Object>();
theHashMap has these data: (pretend this is a for-loop) theHashMap具有以下数据:(假装这是一个for循环)
//1st set
theHashMap.put("BLDG_ID", 111); //int
theHashMap.put("EMP_NAME", "AAA"); //String
theHashMap.put("FLAG", true); //boolean
theVector.add(theHashMap);
//2nd set
theHashMap.put("BLDG_ID", 222); //int
theHashMap.put("EMP_NAME", "BBB"); //String
theHashMap.put("FLAG", false); //boolean
theVector.add(theHashMap);
//2nd set<br>
theHashMap.put("BLDG_ID", 111); //int
theHashMap.put("EMP_NAME", "CCC"); //String
theHashMap.put("FLAG", false); //boolean
theVector.add(theHashMap);
I want to sort the contents of my vector of HashMap according to BLDG_ID so that when I display the data it would look like 我想根据BLDG_ID对我的HashMap向量的内容进行排序,以便在显示数据时看起来像
BLDG_ID || EMP_NAME
111 || AAA
111 || CCC
222 || BBB
How do I do that? 我怎么做?
I think you'd be much better off doing something like this: Instead of using a hashmap for your values, just make a class. 我认为您最好做这样的事情:不用创建一个哈希表作为您的值,只需创建一个类即可。 Then you'll get compile time checking
on your operations, which will help prevent errors down the road. 然后,您将获得compile time checking
操作的信息,这将有助于防止错误的产生。
class Employee implements Comparable<Employee> {
int buildingId;
String name;
boolean flag;
Employee(int b, String n, boolean f) {
buildingId = b;
name = n;
flag = f;
}
public int compareTo(Employee other) {
if(other.buildingId == this.buildingId)
return name.compareTo(other.name);
return buildingId - other.buildingId; // potential for overflow, be careful
}
}
Then you can just sort the vector using whatever sort you want. 然后,您可以使用所需的任何排序对向量进行排序。 If you use ArrayList (the modern form of Vector) you can use Collections.sort(myList);
如果使用ArrayList(Vector的现代形式),则可以使用Collections.sort(myList);
List<Employee> emps = new ArrayList<Employee>();
emps.add(new Employee(111,"AAA",true));
emps.add(new Employee(111,"CCC",false));
emps.add(new Employee(111,"BBB",false));
Collections.sort(emps);
System.out.println("Building Id,Employee Name");
for(Employee emp : emps) System.out.println(emp.getCSV()); // or however you want to format it
Implement a custom Comparator<Map<String, Object>>
, then call Collections.sort 实现自定义Comparator<Map<String, Object>>
,然后调用Collections.sort
Note: You might want to use ArrayList instead of Vector. 注意:您可能要使用ArrayList而不是Vector。
List<Map<String, Object>> vector = new Vector<Map<String, Object>>();
Collections.sort(vector, new Comparator<Map<String, Object>>() {
@Override
public int compare(Map<String, Object> map1, Map<String, Object> map2) {
return ((Integer) map1.get("BLDG_ID")).compareTo((Integer) map2.get("BLDG_ID")));
}
});
Update: For your code: 更新:对于您的代码:
After the "last" 经过“最后”
theVector.add(theHashMap);
add the following 添加以下内容
Collections.sort(theVector, new Comparator<HashMap<String, Object>>() {
@Override
public int compare(HashMap<String, Object> o1, HashMap<String, Object> o2) {
return ((Integer) o1.get("BLDG_ID")).compareTo((Integer) o2.get("BLDG_ID"));
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.