【问题标题】:how to sort a vector of hashmaps如何对哈希图向量进行排序
【发布时间】:2011-10-20 17:37:17
【问题描述】:

我有一个 Hashmap 的 Vector,而 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);

我想根据 BLDG_ID 对 HashMap 向量的内容进行排序,以便在显示数据时看起来像

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

我该怎么做?

【问题讨论】:

  • 我的 >和 <已被剥离这里再次声明 theVector'' Vector <HashMapvString, Object>> theVector= new Vector>();
  • 请学习如何使用 Markdown:stackoverflow.com/editing-help
  • 啊。我的 已被剥离,这里再次声明 theVector'' Vector > theVector= new Vector>(); HashMap theHashMap= new HashMap();

标签: java sorting vector hashmap


【解决方案1】:

我认为你最好做这样的事情:不要为你的值使用哈希图,只需创建一个类。然后,您将在您的操作中收到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

【讨论】:

  • +1:object denial的另一种情况。
  • 不幸的是,我无法更改哈希图,因为其他一些进程需要该对象
  • @Joachim 这是一个术语!
  • @ayeenc 这应该得到解决。如果不出意外,我会在内部将其用于您自己的排序目的。从长远来看,它将简化事情。但实际上,您应该与 hashmap 的使用者交谈并让他们过渡到对象。
  • 我也无法将向量更改为列表...如果我更改任何数据类型,我会破坏许多其他功能/依赖项
【解决方案2】:

实现自定义Comparator&lt;Map&lt;String, Object&gt;&gt;,然后调用Collections.sort

注意:您可能希望使用 ArrayList 而不是 Vector。

【讨论】:

  • 嗨 puce,我不确定我理解你所说的自定义比较器是什么意思。你介意举个例子吗?
  • 看看 Bhesh Gurung 的回答。他给出了这样一个比较器的样本。
【解决方案3】:
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"));
        }            
    });

【讨论】:

  • 使用接口(Map)而不是实现(HashMap)。此外我认为第二个通用参数应该是 Object.
  • @Puce:改了。感谢您的建议。
  • 感谢 Bhesh Gurung 和 puce,请原谅我的无知,但是在给定我的数据样本的情况下,我该如何使用上面的代码?我是否将自定义比较器添加到我拥有 theVector.add(theHashMap) 的相同方法中;和 theHashMap.put("BLDG_ID", 222);//int
    等等?感谢您的耐心等待
  • @ayeenc:是的,当你把所有的HashMaps 添加到向量中之后,你需要对你的向量进行排序吗?
  • @ayeenc:另外,由于这是您在 SO 中的第一个问题,如果您的问题得到解决,您可以接受答案。见meta.stackexchange.com/questions/5234/…
猜你喜欢
  • 2015-01-01
  • 1970-01-01
  • 2017-05-30
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多