在Map中对键和值进行分组

v7pvogib  于 2021-08-20  发布在  Java
关注(0)|答案(4)|浏览(410)

我有一个Map,它的键是string,值是string[]。

Map<String,String[]> map = new HashMap<>();
map.put("String1", new String[]{ "abc", "dfe", "new"});
map.put("String8", new String[]{"xyz","hji","new"});
map.put("String2", new String[]{"abc","dfe","old"});
map.put("String3", new String[]{"abc","dfe","past"});
map.put("String5", new String[]{"xyz","hji","ancient"});
map.put("String6",new String[]{"xyz","hji","past"});
map.put("String4", new String[]{"abc","dfe","ancient"});
map.put("String7", new String[]{"xyz","hji","old"});

我想使用值打印Map分组。
值是字符串的数组。
字符串数组的前两个元素将用于分组。
代码必须找到类似的值并按值分组,而不通过任何过滤器。
最终输出如下所示:

String1:[{ "abc", "dfe", "new"}]
String2:[{"abc","dfe","old"}]
String3:[{"abc","dfe","past"}]
String4:[{"abc","dfe","ancient"}]
String5:[{"xyz","hji","ancient"}]
String6:[{"xyz","hji","past"}]
String7:[{"xyz","hji","old"}]
String8:[{"xyz","hji","new"}]

如何在Java8中实现这一点?

ifmq2ha2

ifmq2ha21#

根据您的输出,您似乎只想根据键进行排序。所以你可以这样做。创建一个 SortedMap 只需将当前Map添加到构造函数中即可。

SortedMap<String,String[]> smap = new TreeMap<>(map);
smap.forEach((k,v)-> System.out.println(k + ":" + Arrays.toString(v)));

印刷品

String1:[abc, dfe, new]
String2:[abc, dfe, old]
String3:[abc, dfe, past]
String4:[abc, dfe, ancient]
String5:[xyz, hji, ancient]
String6:[xyz, hji, past]
String7:[xyz, hji, old]
String8:[xyz, hji, new]
soat7uwm

soat7uwm2#

您可以通过如下方式进行双重分组:

Map<String, String[]> map = new HashMap<>();
map.put("String1", new String[]{"abc", "dfe", "new"});
map.put("String8", new String[]{"xyz", "hji", "new"});
map.put("String2", new String[]{"abc", "dfe", "old"});
map.put("String3", new String[]{"abc", "dfe", "past"});
map.put("String5", new String[]{"xyz", "hji", "ancient"});
map.put("String6", new String[]{"xyz", "hji", "past"});
map.put("String4", new String[]{"abc", "dfe", "ancient"});
map.put("String7", new String[]{"xyz", "hji", "old"});

Map<String, Map<String, List<Map.Entry<String, String[]>>>> collect = map.entrySet()
        .stream()
        .collect(Collectors.groupingBy(x -> x.getValue()[0], Collectors.groupingBy(x -> x.getValue()[1])));

Map<String, String[]> collect1 = collect.values()
        .stream()
        .flatMap(x -> x.values().stream())
        .flatMap(Collection::stream)
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

System.out.println(collect1);
ej83mcc0

ej83mcc03#

如果您只想根据键进行排序,则可以使用java 8:

Map<String, String[]> map = new HashMap<>();
        map.put("String1", new String[]{"abc", "dfe", "new"});
        map.put("String8", new String[]{"xyz", "hji", "new"});
        map.put("String2", new String[]{"abc", "dfe", "old"});
        map.put("String3", new String[]{"abc", "dfe", "past"});
        map.put("String5", new String[]{"xyz", "hji", "ancient"});
        map.put("String6", new String[]{"xyz", "hji", "past"});
        map.put("String4", new String[]{"abc", "dfe", "ancient"});
        map.put("String7", new String[]{"xyz", "hji", "old"});

        map = map.entrySet().stream().sorted(comparingByKey()).collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2, LinkedHashMap::new));

        map.forEach((s1, strings) -> System.out.println(s1 + " : " + Arrays.toString(strings)));

输出将是:

String1 : [abc, dfe, new]
String2 : [abc, dfe, old]
String3 : [abc, dfe, past]
String4 : [abc, dfe, ancient]
String5 : [xyz, hji, ancient]
String6 : [xyz, hji, past]
String7 : [xyz, hji, old]
String8 : [xyz, hji, new]
mzmfm0qo

mzmfm0qo4#

正如您所提到的,您希望根据数组的前2个元素(值)对Map进行排序,然后您可以尝试使用comparator:
根据值对数组的前2个元素进行分组,然后进行排序:

Map<String, String[]> map = new HashMap<>();
    map.put("String1", new String[]{"abc", "dfe", "new"});
    map.put("String8", new String[]{"xyz", "hji", "new"});
    map.put("String2", new String[]{"abc", "dfe", "old"});
    map.put("String3", new String[]{"abc", "dfe", "past"});
    map.put("String5", new String[]{"xyz", "hji", "ancient"});
    map.put("String6", new String[]{"xyz", "hji", "past"});
    map.put("String4", new String[]{"abc", "dfe", "ancient"});
    map.put("String7", new String[]{"xyz", "hji", "old"});

    // Compare based on 1st 2 element oof Array (Map Value)
    Comparator<String[]> byArrayValue = (String[] o1, String[] o2) -> o1[0].concat(o1[1]).compareTo(o2[0].concat(o2[1]));

    // Sort based on 1st 2 element of array
    map = map.entrySet().stream()
            .sorted(Map.Entry.<String, String[]>comparingByValue(byArrayValue))
            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));

    map.forEach((s1, strings) -> System.out.println(s1 + " : " + String.join(",", strings)));

输出将基于值数组的前2个元素:

String4 : [abc, dfe, ancient]
String3 : [abc, dfe, past]
String2 : [abc, dfe, old]
String1 : [abc, dfe, new]
String8 : [xyz, hji, new]
String7 : [xyz, hji, old]
String6 : [xyz, hji, past]
String5 : [xyz, hji, ancient]

相关问题