簡體   English   中英

在 Map 中分組鍵和值

[英]Grouping Keys and Values in Map

我有一個 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"});

我想使用值打印地圖分組。
值是字符串數組。
String 數組的前兩個元素將用於分組。
代碼必須在不通過任何過濾器的情況下找到相似的值並按值分組。
最終輸出將如下所示:

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"}]

如何在 Java 8 中實現這一點?

根據您的輸出,您似乎只想根據鍵進行排序。 所以你可以這樣做。 創建一個SortedMap並在構造函數中將當前地圖添加到它。

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]

你可以像這樣進行雙重分組:

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

如果您只想根據鍵進行排序,那么您可以使用 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]

同樣正如您提到的,您想根據數組的第 2 個元素(值)對地圖進行排序,然后您可以嘗試比較器:

從值分組數組的第 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]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM