如何对哈希图向量进行排序

发布于 2024-12-11 05:11:15 字数 1055 浏览 0 评论 0 原文

我有一个 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);

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

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

我该怎么做?

i have a Vector of Hashmap and the HashMap contains different data types:

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

theResults contains this HashMap:

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

theHashMap has these data:
(pretend this is a for-loop)

//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 || EMP_NAME
111     ||    AAA
111     ||    CCC
222     ||    BBB

How do I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

£烟消云散 2024-12-18 05:11:15

我认为你最好这样做:不要使用哈希图作为你的值,而是创建一个类。然后,您将对操作进行编译时检查,这将有助于防止出现错误。

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

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.

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

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
雨落□心尘 2024-12-18 05:11:15

实现自定义 Comparator>,然后调用 Collections.sort

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

Implement a custom Comparator<Map<String, Object>>, then call Collections.sort

Note: You might want to use ArrayList instead of Vector.

始终不够爱げ你 2024-12-18 05:11:15
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"));
        }            
    });
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"));
        }            
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文