简体   繁体   中英

How to convert a list of java objects to Map<String, Map<String, String>>

I have a list of java objects as below:

[
  {
    id: "frwfhfijvfhviufhbviufg",
    country_code: "DE",
    message_key: "key1",
    translation: "This is the deutsch translation"
  },
  {
    id: "dfregregtegetgetgttegt",
    country_code: "GB",
    message_key: "key1",
    translation: "This is the uk translation"
  },
  {
    id: "frffgfbgbgbgbgbgbgbgbg",
    country_code: "DE",
    message_key: "key2",
    translation: "This is the again deutch translation"
  }
]

How can I convert this into a Map<String, Map<String, String>> like below:

{
  "DE": {
    "key1": "This is the deutsch translation",
    "key2": "This is the again deutch translation"
  },
  "GB": {
    "key1": "This is the uk translation"
  }
}

I am new to java and below is my code but the code is not correct:

Map<String, Translations> distinctTranslations = customTranslationsEntities
        .stream().collect(Collectors.groupingBy(
                CustomTranslationsEntity::getCountryCode,
                Collectors.toMap(
                        CustomTranslationsEntity::getMessageKey,
                        CustomTranslationsEntity::getTranslation),

                )))

where Translations is proto buffer message like below:

message Translations {
  map<string, string> translations = 1;
}

Here map<string, string> translations means map like "key1", "This is the deutsch translation" ...like this.

The output should be Map<String, Map<String,String>> :

Map<String, Map<String,String>>
    distinctTranslations = customTranslationsEntities
            .stream()
            .collect(Collectors.groupingBy(CustomTranslationsEntity::getCountryCode,
                                    Collectors.toMap(
                                            CustomTranslationsEntity::getMessageKey,
                                            CustomTranslationsEntity::getTranslation,
                                            (v1,v2)->v1)));

I added a merge function, in case there are duplicate keys.

If you want to do it without using streams then

private List<MyObject> list = // Your object List
private Map<String, Map<String, String>> map = new HashMap<>();

for(MyObject object : list){
    Map<String, String> localMap;
    localMap = map.getOrDefault(object.country_code, new HashMap<>());
    localMap.put(object.message_key, object.translation);
    if(!map.containsKey(object.country_code)){
        map.put(object.country_code, localMap);
    }
}

Your code is correct and working. Just add a merge function to avoid getting the IllegalStateException for duplicate keys.

Update the Collector.toMap() in this way :

Collectors.toMap(..., (trans1, trans2) -> trans1))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM