I have the following code that I’d like to try to write using the java collectors. Given 2 attributes (firstname and lastname) of a person, I’d like to get a map containing the unique firstname or lastname as a key, and the list of the corresponding persons. Here’s a set of data : Output (as expected) is the following :
Tag: java-stream
Does the order of applying comparators and predicates matter?
We have a web page, where the user can select an arbitrary number of rules for filtering and sorting a group of items. With each rule, he sets a priority for it, with which the user defines the order of how the rules should be applied. From this group of rules I extract Comparator (if the rule was sorting) and/or
Using Lambda and Streams in For each and returning result
i am have been learning lambda and streams lately and have kind of been thrown into the deep end really early. I currently have an array list of books, a user types in a word and if the word equals the books author or title, the books toString(all attributes of the book nicely formatted) is called and returned. Very easy
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