【问题标题】:Sort Map of a Map based on property根据属性对 Map 的 Map 进行排序
【发布时间】:2014-05-15 08:33:49
【问题描述】:

我正在尝试排序:

Map<String,Map<String,Object>> 基于属性(dispSeq)

数据的一个例子是:

ProductType={BeanTag:'xxx',dispSeq=10,.....}

ChargeType={BeanTag:'yyy',dispSeq=30.1,.....}

Something={BeanTag:'zzz',dispSeq=29,.....}

所以,如上所述,我想根据 dispSeq 对其进行排序。

我知道数据结构不是执行排序的最佳选择......但在这一点上,我无能为力。

我可以通过以下方式对“内部”地图进行排序:

private void orderColumns(Map columnDetails) {

    List<Map<String, Object>> columnsValues = new ArrayList<Map<String, Object>>(
            columnDetails.values());

    Collections.sort(columnsValues, new Comparator<Map<String, Object>>() {
        public int compare(Map<String, Object> o1, Map<String, Object> o2) {

            if (Integer.parseInt(o1.get("dispSeq").toString()) == Integer
                    .parseInt(o2.get("dispSeq").toString()))
                return 0;
            return Integer.parseInt(o1.get("dispSeq").toString()) < Integer
                    .parseInt(o2.get("dispSeq").toString()) ? -1 : 1;
        }
    });

}

但这不会起作用,因为我失去了主地图的钥匙。

有什么想法吗?

【问题讨论】:

  • 当你说o2.get("dispSeq")这是否意味着你的对象的关键是dispSeq?根据您的示例,这是您价值对象中的一个归档?他们都一样吗?
  • o2.get("dispSeq") ,是检索值(数字)进行排序。关键是“dispSeq”。
  • 这张地图有两种排序方式。根据来自 ProductType、ChargeType 等对象的 dispSeq 值对内部映射进行排序。或者根据内部映射中值对象的 dispSeq 对外部映射进行排序。在后一种情况下,排序将仅比较内部映射中第一项的值。您在这里的要求是哪一个?
  • 对外部地图进行排序将是我认为的最佳解决方案。但我无法以合乎逻辑的方式思考:S。

标签: java sorting map


【解决方案1】:

根据我上面的评论,这是对地图进行排序的两种方式。

乍一看可能看起来很混乱,但这里的想法是获取条目集并将其转换为列表并使用Collections.sort()方法对列表进行排序。对于外部地图情况,我获取条目集,并在比较方法中从地图中获取第一个元素并进行比较。您可以按照适合您的方式更改比较逻辑。

我假设你有一个 ProductType 类型的对象,它有一个字段 dispSeq,它是 int/Integer。因此,如果您有多个对象,如 ProductType、ChargeType 等。您可以使用比较实例进行强制转换,或者使用 getDispSeq() 方法为所有相关对象提供接口;

希望这会有所帮助。

private static Map<String, Object> sortInnerMap(Map<String, Object> unsortedMap) {

    List<Entry<String, Object>> list = new LinkedList<Entry<String, Object>>(unsortedMap.entrySet());

    Collections.sort(list, new Comparator<Entry<String, Object>>() {
        public int compare(Entry<String, Object> o1, Entry<String, Object> o2) {
            return ((Comparable) ((ProductType) o1.getValue()).getDispSeq())
                                   .compareTo(((ProductType) o2.getValue()).getDispSeq());
        }
    });

    Map<String, Object> sortedMap = new LinkedHashMap<String, Object>();
    for(Entry<String, Object> item : list){
        sortedMap.put(item.getKey(), item.getValue());
    }
    return sortedMap;
}

private static Map<String, Map<String, Object>> sortOuterMap(
        Map<String, Map<String, Object>> unsortedMap) {

    List<Entry<String, Map<String, Object>>> list = new LinkedList<Entry<String, Map<String, Object>>>(
            unsortedMap.entrySet());

    Collections.sort(list,
            new Comparator<Entry<String, Map<String, Object>>>() {
                public int compare(Entry<String, Map<String, Object>> o1,
                        Entry<String, Map<String, Object>> o2) {
                    return ((Comparable) ((ProductType) o1.getValue()
                            .entrySet().iterator().next().getValue())
                            .getDispSeq()).compareTo(((ProductType) o2.getValue()
                            .entrySet().iterator().next().getValue())
                            .getDispSeq());
                }
            });

    Map<String, Map<String, Object>> sortedMap = new LinkedHashMap<String, Map<String, Object>>();
    for(Entry<String, Map<String, Object>> item : list){
        sortedMap.put(item.getKey(), item.getValue());
    }
    return sortedMap;
}

【讨论】:

    【解决方案2】:

    不要根据内部地图对其进行排序。使用Collections.sort() 对外部地图进行排序,并相应地更改比较方法以从内部地图中获取dispSeq 的值并相应地返回值。

    【讨论】:

      猜你喜欢
      • 2013-06-27
      • 2016-01-21
      • 2021-03-26
      • 2016-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-18
      • 2012-07-23
      相关资源
      最近更新 更多