简体   繁体   English

从列表创建HashMap <String>

[英]Create HashMap from List<String>

I have a few simple JSON files that have syntax 我有一些具有语法的简单JSON文件

"key1": "value1"
"key2": "value2"

etc. 等等

I need to create a Map from each of them, and then merge them into one. 我需要从它们每个创建一个Map,然后将它们合并为一个。 The following code works as is supposed to: 以下代码按预期工作:

private TranslationBundle assembleBundle(List<String> translations) {
        TranslationBundle translationBundle = new TranslationBundle();
        Map<String, String> bundledTranslationMap = new HashMap<>();

        translations.forEach(translation -> {
                bundledTranslationMap.putAll(getSingleTranslationMap(translation).);
        });
        translationBundle.setTranslationMap(bundledTranslationMap);
        return translationBundle;
    }

    private Map<String, String> getSingleTranslationMap(String translation){
        ObjectMapper mapper = new ObjectMapper();
        try{
            return mapper.readValue(translation, new TypeReference<Map<String, String>>(){});
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

What I want to achieve is to rewrite the code in the assembleBundle method into a more functional one, like 我要实现的是将assembleBundle方法中的代码重写为功能更强大的代码,例如

translations.stream().
     map(this::getSingleTranslationMap)
     .collect(collect to single HashMap);

In the collect() method, as far as I know, I have to somehow access the key and value for each result of getSingleTranslationMap . 据我所知,在collect()方法中,我必须以某种方式访问getSingleTranslationMap每个结果的键和值。 Since there is no variable name assigned to the result of this method, how should it be done? 由于没有为该方法的结果分配变量名,应该怎么做? Maybe I'm trying a wrong approach at all? 也许我正在尝试一种错误的方法?

You can transform the individual Map s returned by getSingleTranslationMap into a Stream of map entries, and collect them into a single Map : 您可以将getSingleTranslationMap返回的各个Map转换为Map条目的Stream ,并将它们收集到一个Map

Map<String, String> bundledTranslationMap =
    translations.stream()
                .flatMap(translation -> getSingleTranslationMap(translation).entrySet()
                                                                            .stream())
                .collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue));

Of course, if getSingleTranslationMap returns a Map with a single Entry , it might make more sense for that method to return a Map.Entry instead of a Map , which would simplify the above Stream pipeline. 当然,如果getSingleTranslationMap返回具有单个EntryMap ,则该方法返回Map.Entry而不是Map可能更有意义,这将简化上述Stream管道。

PS, if duplicate keys are possible, you should add a merge function to the toMap() call. PS,如果可能有重复的键,则应将合并功能添加到toMap()调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从一个自定义 object 列表中创建一个 HashMap,它将 String 作为键,值将是另一个 HashMap? - How to create a HashMap that would have String as key and the value would be another HashMap from one list of custom object? 从HashMap值创建String [] - Create String[] from HashMap values 给定一个字符串列表,创建一个具有长度和字符串的哈希表 - Given a list of strings, create a hashmap with length and string 从 HashMap 创建多维 json<String, String> - Create multidimensional json from HashMap<String, String> 创建HashMap <String,Map> 从列表&lt;地图 <String,Object> &gt;给出java.lang.ClassCastException - Create HashMap<String,Map> from List < Map<String,Object>> gives java.lang.ClassCastException 如何从HashMap获取列表 <String,List<String> &gt;到另一个列表 - How to fetch list from HashMap <String,List<String>> to another list 从列表中提取条目 <String> 嵌套在HashMap中 - Pull entry from List<String> nested in HashMap 从嵌套的 HashMap 中获取 HashMapsValues <string, hashmap<string, string> &gt; 根据列表过滤<string></string></string,> - Get HashMapsValues from nested HashMap<String, HashMap<String, String>> filtered against a List<String> 使用现有列表中HashMap中的值创建一个新列表 - Create a new list with values from HashMap from existing list 列出到Hashmap以创建API - List to Hashmap to create API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM