如何排序包含HashMap

问题描述:

的向量我有哈希映射的向量和HashMap中包含不同的数据类型:如何排序包含HashMap

Vector<HashMap<String, Object>> theVector= new Vector<HashMap<String, Object>>(); 

theResults包含此HashMap:

HashMap<String, Object> theHashMap= new HashMap<String, Object>(); 

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); 

我想排序我的向量的内容HashMap中根据BLDG_ID这样,当我显示的数据会看起来像

BLDG_ID || EMP_NAME 
111  || AAA 
111  || CCC 
222  || BBB 

我该怎么办呢?

+0

我>和<在这里已经剥离是为theVector声明再次' 矢量< HashMapvString,对象> > theVector =新的向量<的HashMap <字符串,对象> >(); –

+0

请了解如何使用Markdown:http://*.com/editing-help – NullUserException

+0

argh。我的已被剥离,这里是向量的声明再次'' Vector > theVector = new Vector >(); HashMap theHashMap = new HashMap (); –

实现自定义Comparator<Map<String, Object>>,然后的调用Collections.sort

注意:您可能需要使用ArrayList,而不是矢量。

+0

嗨puce,即时通讯不知道我明白你是什么意思的自定义比较器..你会介意给一个例子吗? –

+0

看看Bhesh Gurung的回答。他给了这样一个比较器的样本。 – Puce

我认为你做这样的事情会好得多:不要为你的值使用散列表,只要创建一个类。然后,您的业务将得到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 
    } 

} 

然后,你可以使用任何你想要的排序矢量。如果您使用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 
+0

+1:[object denial]的另一种情况(http://*.com/questions/3725703/how-to-store-more-than-one-string-in-a-map)。 –

+0

不幸的是,我不能改变哈希映射,因为一些其他进程需要对象 –

+0

@Joachim这是一个术语的宝石! – corsiKa

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"))); 
    }    
}); 

更新:为您的代码:

“最后”

theVector.add(theHashMap); 

后添加以下

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")); 
     }    
    }); 
+0

使用接口(Map)而不是实现(HashMap)。除此之外,我认为第二个通用参数应该是Object。 – Puce

+0

@Puce:改变它。感谢您的建议。 –

+0

感谢Bhesh Gurung&puce, 请原谅我的无知,但我如何使用上面的代码给出我的数据样本?我在同样的方法里添加了自定义比较器,我有theVector.add(theHashMap);和HashMap.put(“BLDG_ID”,222); // int
等? 感谢您的耐心等待 –