【问题标题】:Sort HashMap basis on Object's member variable from value [duplicate]根据对象的成员变量从值中对HashMap进行排序[重复]
【发布时间】:2013-03-10 14:57:51
【问题描述】:

上一节课

class Employee {
    int id;
    String name;
}

和一个包含这个对象的地图

Map<Integer, Employee> map = new HashMap<Integer, Employee>();

现在我想在map 之上排序一个Employee's name 的基础。意味着当我使用Map.Entry 迭代此地图时,Employee 对象必须按字母顺序检索。

提前致谢

【问题讨论】:

  • 发布您尝试过的样品。
  • 我没试过。我只是搜索这个要求,但没有找到任何相同的帖子。
  • 我想你可以在这篇文章中找到你的答案:How to sort a Map<Key, Value> on the values in Java?
  • @Michaël,感谢您的回复,它对我有用。做了一些改变。在 ANS 中发布更改。谢谢

标签: java sorting collections hashmap


【解决方案1】:

【讨论】:

  • 此方法在合同上用于基于键的排序,尽管它是可能的。
  • @NilsH:由于它允许自定义比较器,您可以编写自己的逻辑进行排序。
  • 是的,你可以,但关键是合约是按key排序的。
  • @NilsH:只要你能有效地完成它并节省额外的编码,我想应该没问题。
  • 当然,我对解决方案没有异议 :) 我只是指出来让原始发帖人意识到这一点。
【解决方案2】:

您无法对HashMap 进行排序,但您可以对使用entrySet() 获得的条目进行排序。

public class MapSort {
    private static class Employee {
        public String name;

        public Employee(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
           return name;
        }
    }

    public static void main(String[] args) {
        Map<Integer, Employee> map = new HashMap<Integer, Employee>();

        map.put(1, new MapSort.Employee("x"));
        map.put(2, new MapSort.Employee("a"));
        map.put(3, new MapSort.Employee("f"));

        List<Map.Entry<Integer, Employee>> entryList = new ArrayList<Map.Entry<Integer, Employee>>(map.entrySet());

            Collections.sort(
                    entryList, new Comparator<Map.Entry<Integer, Employee>>() {
                @Override
                public int compare(Map.Entry<Integer, Employee> integerEmployeeEntry,
                                   Map.Entry<Integer, Employee> integerEmployeeEntry2) {
                    return integerEmployeeEntry.getValue().name
                            .compareTo(integerEmployeeEntry2.getValue().name);
                }
            }
        );

        System.out.println(entryList);
    }
}

排序后,您可以将条目放回支持排序的地图中,例如LinkedHashMap

这取决于您的用例:如果您需要始终对地图进行排序,则使用 TreeMap 会更简单,但会带来额外的开销。如果您只需要一次排序,您可以在上面的代码中使用HashMap

【讨论】:

    【解决方案3】:

    Michaël 建议了一个链接 Sort a Map<Key, Value> by values (Java) 。我对它做了一些改变。它对我有用

    class ValueComparator implements Comparator<Integer> {
    
        Map<Integer, Employee> base;
        public ValueComparator(Map<Integer, Employee> base) {
            this.base = base;
        }
    
        // Note: this comparator imposes orderings that are inconsistent with equals.    
        public int compare(Integer a, Integer b) {
            return ((Employee)base.get(a)).compareTo(base.get(b));
        }
    
    }
    
    class Employee implements Comparable {
        public String name;
        public int id;
    
        Employee(int id, String name) {
            this.id = id;
            this.name = name;
        }
    
        @Override
        public int compareTo(Object obj) {
            return this.name.compareTo(((Employee)obj).name);
        }
    
        public String toString() {
            return name;
        }
    }
    

    解决方法也请参考上面的链接。

    感谢所有回复的人。

    【讨论】:

      猜你喜欢
      • 2011-02-18
      • 2011-09-09
      • 1970-01-01
      • 1970-01-01
      • 2014-05-29
      • 2013-05-13
      • 2017-04-06
      • 2016-01-21
      • 2011-12-28
      相关资源
      最近更新 更多