【问题标题】:Sort the list of maps by key Value pair in java在java中按键值对对映射列表进行排序
【发布时间】:2021-12-23 01:03:11
【问题描述】:

我有一个 JSON 文档。我已将文档转换为 Map 其中一些键具有 Maps (List>) 列表作为值。我想对这个 List 进行排序。

只是一个例子:(将JSON文档存储在Map之后

1. key: name, value: {first=John, last=Doe}
2. key: address, value: null
3. key: birthday, value: 1980-01-01
4. key: company, value: Acme
5. key: occupation, value: Software engineer
6. key: phones, value: [{number=9, type=mobile1}, {number=1, type=mobile}, {type=home, number=0}]
7. key: groups, value: [gym, close-friends]

在上面的示例中,第 6 行 Key = "phones" 作为我必须排序的地图列表具有价值。

预期输出:

1. key: name, value: {first=John, last=Doe}
2. key: address, value: null
3. key: birthday, value: 1980-01-01
4. key: company, value: Acme
5. key: occupation, value: Software engineer
6. key: phones, value: [{number=0, type=home}, {number=1, type=mobile}, {number=9, type=mobile1}]
7. key: groups, value: [gym, close-friends]

【问题讨论】:

  • 这可能是使用 JSON 框架(例如 Jackson)最简单的方法,并创建一个可以将 JSON 映射到的 Java 对象
  • 代替 Map 创建一个带有必要字段的 Java 对象,并将获取的 JSON 映射到该对象。

标签: java list dictionary


【解决方案1】:

你可以使用List.sort():

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class Main {
    private static final String TYPE = "type";
    private static final String NUMBER = "number";

    public static void main(String[] args) {
        Map<String, List<Map<String, String>>> jsonMap = new HashMap<>();
        jsonMap.put("phones", new ArrayList<>(Arrays.asList(
                                    Map.of(NUMBER, "9", TYPE, "mobile1"), 
                                    Map.of(NUMBER, "1", TYPE, "mobile"),
                                    Map.of(TYPE, "home", NUMBER, "0"))));
        
        System.out.println("Before:");
        System.out.println(jsonMap);

        jsonMap.get("phones").sort((a, b) -> 
            Integer.valueOf(a.get(NUMBER)).compareTo(Integer.valueOf(b.get(NUMBER))));

        System.out.println("After:");
        System.out.println(jsonMap);
    }

}

输出:

Before:
{phones=[{type=mobile1, number=9}, {type=mobile, number=1}, {type=home, number=0}]}
After:
{phones=[{type=home, number=0}, {type=mobile, number=1}, {type=mobile1, number=9}]}

【讨论】:

    【解决方案2】:

    我想对这个 List 进行排序。

    鉴于您显示的数据,所有地图都只有一个条目。所以我们按这个条目的键排序。特别是他们的integer 值。

    List<Map<String, String>> myMapList 
          = // get list of maps here
        Collections.sort(myMapList, new Comparator<Map<String, String>>() {
            @Override
            public int compare(
              Map<String, String> m1, Entry<String, String> m2) {
                var key1 = m1.keySet().iterator().next();
                var key2 = m2.keySet().iterator().next()
                return Integer.valueOf(key1).compareTo(Integer.valueOf(key2));
            }
        });
    

    更多详情请查看this

    这能解决您的问题吗?在 cmets 中告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-01
      • 1970-01-01
      • 2018-01-15
      • 2017-11-15
      • 2015-07-17
      • 2019-12-30
      相关资源
      最近更新 更多