Skip to content
Advertisement

Tag: java-stream

Kotlin Stream peek(…) method

What is the best alternative in Kotlin to java.util.stream.Stream<>.peek(…)? https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#peek-java.util.function.Consumer- Seems there are no alternative intermediate operations: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.streams/index.html I found only terminating forEach(…) Answer The Stream alternative in Kotlin is Sequences. There’s onEach to do what peek does. Fun fact: Kotlin also wanted to call their sequences “Streams” before it was clear that Java would do the same, so they

Flatmap nested collection

I have a list of objects, some of them can be collections. I would like to get a stream of plain objects. I would like to get a stream with elements. I have tried I also checked an example which shows how to use a recursive function which flattens a collection. However, in this example .collect(Collectors.toList()); used to keep an

handle duplicate key in Collectors.toMap() function

I am creating a map which its (key,value) will be (name, address) in my Person object: In the duplicate key situation, I would like to skip to add the second address to the map and would like to log the name also. Skipping the duplicate address I can do already using mergeFunction, but in oder to log the name I

Java stream find value by key if exists

I’m having simple DataStructure And I need to return value from `List’ based on key and I want to do it Java8 way, with streams. I think code speaks for himself: How can I modify findValueStream() to not throw NoSuchValueException while I search for non existing key? I don’t want to return Optional<String> because this method is already used in

Java 8: How to convert String to Map?

I have a Map: I converted it to a String: How to convert utilMapString to Map in Java8? Who can help me with? Answer Split the string by , to get individual map entries. Then split them by = to get the key and the value. Note: As pointed out by Andreas@ in the comments, this is not a reliable

Is Java 8 stream laziness useless in practice?

I have read a lot about Java 8 streams lately, and several articles about lazy loading with Java 8 streams specifically: here and over here. I can’t seem to shake the feeling that lazy loading is COMPLETELY useless (or at best, a minor syntactic convenience offering zero performance value). Let’s take this code as an example: This will log in

Should I use shared mutable variable update in Java 8 Streams

Just iterating below list & adding into another shared mutable list via java 8 streams. What is the difference between above three iteration & which one we need to use. Are there any considerations? Answer Functionally speaking,for the simple cases they are almost the same, but generally speaking, there are some hidden differences: Lets start by quoting from Javadoc of

Advertisement