简体   繁体   English

Java 8 个流 - 在 stream 操作期间调用“映射”而不保存到变量

[英]Java 8 Streams - Call "map" during stream operations without saving to variable

I could not find many answers to this, though I suppose its not possible, but I was curious - is there anyway in Java8 streams to "refer" or to "call" the HashMap that is created via the .collectors(Collectors.toMap(key -> "key", val -> "val") ? I know I can save this resultant reduction which creates the Map to Map<String, String> myMap variable, but I still had to perform operations on this resultant HashMap and I wanted to keep the stream going, so I wanted to do something like this:我找不到很多答案,尽管我认为它不可能,但我很好奇 - 在 Java8 流中是否有“参考”或“调用”通过.collectors(Collectors.toMap(key -> "key", val -> "val") ?我知道我可以保存这个产生的减少,它创建 Map 到Map<String, String> myMap变量,但我仍然必须对这个结果 HashMap 和我执行操作想让 stream 继续运行,所以我想做这样的事情:

myHashSet.stream().
.filter(i -> i != null)
.collectors(Collectors.toMap(key -> "key", val -> "val")
.forEach((k,v) -> {

   // HERE IS WHERE I WANT TO CALL THE MAP CREATED ABOVE
   if( MAP.contains("someRandomValue") {
    
    }


}));

I'm assuming you can't do this, but I was hoping there was some method or something so I don't have to "kill" the stream, ie save the Map to a variable, then proceed to stream it out again etc...我假设你不能这样做,但我希望有一些方法或其他东西,所以我不必“杀死”stream,即将 Map 保存到一个变量,然后再继续执行 ZF7B44CFAFD159C5222E7B584 ...

The collect operation in Stream is a terminal operation. Stream中的collect操作是终端操作。 So there is no Stream after collect .所以collect之后没有Stream You would have to do something like below.您必须执行以下操作。

var myMap = myHashSet.stream().
.filter(i -> i != null)
.collectors(Collectors.toMap(key -> "key", val -> "val");

myMap.forEach((k,v) -> {

   // HERE IS WHERE I WANT TO CALL THE MAP CREATED ABOVE
   if( MAP.contains("someRandomValue") {
    
    }


}));

Below article describes about the available intermediate operations in Stream .下面的文章描述了Stream中可用的中间操作。

https://www.javacodegeeks.com/2020/04/java-8-stream-intermediate-operations-methods-examples.html https://www.javacodegeeks.com/2020/04/java-8-stream-intermediate-operations-methods-examples.html

There is no reason that you need to assign the collected map to a local variable, you could simply follow on with use of forEach or a second stream() :没有理由需要将收集的 map 分配给局部变量,您可以简单地继续使用forEach或第二个stream()

myHashSet.stream().
.filter(Objects::nonNull)
.collectors(Collectors.toMap(key -> "key", val -> "val").
.entrySet().stream()  // or .forEach()
// Add operations on the second stream of Map.Entry here...

However the above creates a second stream.然而,上面创建了第二个 stream。 You can handle as just one stream by mapping as Map.entry() and do other intermediate operations before collect or other stream termination:您可以通过映射为Map.entry()只处理一个 ZF7B44CFFAFD5C52223D5498196C8A2E7BZ 并在collect或其他 stream 终止之前执行其他中间操作:

var map = myHashSet.stream()
.filter(Objects::nonNull)
.map(s -> Map.entry(s, "val"+s))
// Add operations on the stream of Map.Entry here...
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));  // or other termination

The Map.entry() call is JDK9+, for earlier JDK you can use new AbstractMap.SimpleImmutableEntry(key,value) instead. Map.entry()调用是 JDK9+,对于早期的 JDK,您可以使用new AbstractMap.SimpleImmutableEntry(key,value)代替。

I solved this with @shmosel's help using collectingAndThen .我使用collectingAndThen在@shmosel 的帮助下解决了这个问题。 This was the closest to what I wanted to do.这是最接近我想做的事情。

myArrayList.stream().
.collect(Collectors.collectingAndThen(Collectors.toMap(key -> key.getName(), val -> val), map -> map.forEach((key, val) -> System.out.println(map.contains(key)));

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

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