简体   繁体   English

将EntrySet流收集到LinkedHashMap

[英]Collect stream of EntrySet to LinkedHashMap

I want to collect the stream to a LinkedHashMap<String, Object> . 我想将流收集到LinkedHashMap<String, Object>

I have a JSON resource that is stored in LinkedHashMap<String, Object> resources . 我有一个存储在LinkedHashMap<String, Object> resourcesJSON LinkedHashMap<String, Object> resources Then I filter out JSON elements by streaming the EntrySet of this map. 然后我通过流式传输此地图的EntrySet来过滤掉JSON元素。 Currently I am collecting the elements of stream to a regular HashMap . 目前我正在将流的元素收集到常规HashMap But after this I am adding other elements to the map. 但在此之后我将其他元素添加到地图中。 I want these elements to be in the inserted order. 我希望这些元素处于插入顺序。

final List<String> keys = Arrays.asList("status", "createdDate");

Map<String, Object> result = resources.entrySet()
        .stream()
        .filter(e -> keys.contains(e.getKey()))
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

result.put("date", "someDate");
return result;

That is why I want to collect the stream to a LinkedHashMap<String, Object> . 这就是我想要将流收集到LinkedHashMap<String, Object> How can I achieve this? 我怎样才能做到这一点?

You can do this with Stream : 你可以用Stream做到这一点:

Map<String, Object> result = resources.entrySet()
            .stream()
            .filter(e -> keys.contains(e.getKey()))
            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (x, y) -> y, LinkedHashMap::new));

The part (x, y) -> y is because of mergeFunction when find duplicate keys, it returns value of second key which found. 部分(x, y) -> y是因为mergeFunction找到重复键时,它返回找到的第二个键的值。 the forth part is mapFactory which a supplier providing a new empty Map into which the results will be inserted. 第四部分是mapFactory ,供应商提供一个新的空Map,结果将插入其中。

An alternate way of doing this using Map.forEach is: 使用Map.forEach执行此操作的另一种方法是:

Map<String, Object> result = new LinkedHashMap<>();
resources.forEach((key, value) -> {
    if (keys.contains(key)) {
        result.put(key, value);
    }
});
result.put("date", "someDate");

and if you could consider iterating on the keySet as an option: 如果你可以考虑迭代keySet作为一个选项:

Map<String, Object> result = new LinkedHashMap<>();
resources.keySet().stream()
        .filter(keys::contains)
        .forEach(key -> result.put(key,resources.get(key)));
result.put("date", "someDate");

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

相关问题 LinkedHashMap entrySet的顺序未保存在流中(Android) - LinkedHashMap entrySet's order not being preserved in a stream (Android) LinkedHashMap中的entrySet()是否也保证了订单? - Does entrySet() in a LinkedHashMap also guarantee order? Java 8:更改EntrySet的值 stream - Java 8: Change the values of an EntrySet stream 将entrySet流传输到groupingBy而不是keySet - Stream an entrySet to groupingBy instead of keySet 排序LinkedHashMap <String, String[]> .entrySet()由键组成,但在键中间使用正则表达式 - Sorting LinkedHashMap<String, String[]>.entrySet() by the keys, but in the middle of the keys using a regex HashMap.entrySet()和LinkedHashMap.entrySet()的结果有什么不同,它们是否具有相同的性能? - What's the different between the result of HashMap.entrySet() and LinkedHashMap.entrySet(),do they have the same performance? 流对比 Map 的 entrySet 中的迭代器 - Java 8 - Stream Vs. Iterator in entrySet of a Map - Java 8 收集字符串到 LinkedHashMap<character, integer> , 字符出现频率</character,> - Collect String to LinkedHashMap <Character, Integer> , with frequency of characters 使用mybatis的resultmap将对象收集到一个linkhashmap中 - Using resultmap of mybatis to collect objects into a linkedhashmap 从 LinkedHashMap 获取有序流 - Get ordered Stream from LinkedHashMap
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM