簡體   English   中英

使用Java將地圖數據分組到嵌套HashMap中

[英]Group List of Map Data into Nested HashMap in Java

輸入:我有一個Map(鍵-值對)和分組鍵的列表

例如List<Map<String,String>> inputData json

[
 {
    'a': 'German',
    'b': 'Audi',
    'e': 'T3'
 },
 {
    'a': 'German',
    'b': 'BMW',
    'c': 'T6'
 },
 {
    'a': 'American',
    'b': 'Tesla',
    'c': 'T6'
 }
]

和groupingKeys = a and b

對於分組鍵a->我將創建兩個存儲桶,一個用於德國和其他美國人,然后在每個存儲桶中再次創建值b的存儲桶,請檢查以下內容

我想產生以下內容: Map<String, Map<String, List<Map>> - Map<String, Map<String, List<Map>>的嵌套==分組鍵的數量,這里是2

{
   German: {
       Audi: [
            {
               'a': 'German',
               'b': 'Audi',
               'e': 'T3'
            }
       ],
       BMW: [
            {
               'a': 'German',
               'b': 'BMW',
               'c': 'T6'
            }
       ]
   },
   American: {
      Tesla: [
          {
            'a': 'American',
            'b': 'Tesla',
            'c': 'T6'
          }
      ]
   }
}

我嘗試過的事情:

inputData.stream().collect(Collectors.groupingBy(mapItem -> this.constructGroupingKey(groupingKeys, mapItem)));

//constructGroupingKey - this joins the values of of given grouping keys

這樣,我可以構造級聯鍵,例如German-Audi或German-BMW,並針對它保存匹配項,但這並不是我想要的。

就像@Naman在這里對具體類說的那樣-以下內容使它成為一種班輪,但我無法對數據做出假設,它屬於Map類型

Map<String, Map<String, List<ConcreteClass>>> output = inputData.stream().collect(Collectors.groupingBy(ConcreteClass::getCountry, Collectors.groupingBy(ConcreteClass::getCarType)));

希望其他想法,謝謝。 我也很好奇我是否可以在不指定分組鍵的情況下基於給定的數據派生分組。

輸入具有動態性質,鍵和值由輸入定義

只需雙擊輸入作為具體類的表示即可。 它將看起來更加簡單和干凈:

Map<String, Map<String, List<ThrowAway>>> multipleGrouping(List<ThrowAway> inputData) {
    return inputData.stream()
            .collect(Collectors.groupingBy(ThrowAway::getA,
                    Collectors.groupingBy(ThrowAway::getB)));
}

我剛剛創建了一個與您的地圖表示形式相同的示例類:

class ThrowAway {
    String a;
    String b;
    String c;

    String getA() {
        return a;
    }

    String getB() {
        return b;
    }
}

由於您正在尋找動態解決方案,因此這是另一種方法:

您可以使用以下分組方法為以下兩個密鑰傳遞密鑰:

public Map<String, Map<String, List<Map<String, String>>>> group(List<Map<String, String>> list, String key1, String key2) {
    return list.stream().collect(
            Collectors.groupingBy(m -> m.get(key1),
                    Collectors.groupingBy(m -> m.get(key2))
            )
    );
}

這里的問題是,如果該鍵不存在於地圖中,則會得到NullPointerException 為防止這種情況,您可以將map.getOrDefault()與默認鍵一起使用:

public Map<String, Map<String, List<Map<String, String>>>> group(List<Map<String, String>> list, String key1, String key2, String noneKey) {
    return list.stream().collect(
            Collectors.groupingBy(m -> m.getOrDefault(key1, noneKey),
                    Collectors.groupingBy(m -> m.getOrDefault(key2, noneKey))
            )
    );
}

使用以下選項調用方法之一:

Map<String, Map<String, List<Map<String, String>>>> result = group(list, "a", "b");

要么

Map<String, Map<String, List<Map<String, String>>>> result = group(list, "a", "b", "none");

暫無
暫無

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

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