Linked Questions
34 questions linked to/from What's the difference between map() and flatMap() methods in Java 8?
1 vote
2 answers
3k views
how internally map and flatmap works in java 8 [duplicate]
In java 8, when we should go for Stream.map and flat map methods ? I am bit confused about the use cases. Please give some scenarios to use these two methods.
0 votes
1 answer
461 views
How to get List of List of Objects with Java Streams [duplicate]
I can't get my head around this problem. Problem I want to writhe this with Java streams: List<Manufacturer> manufacturers = ... // List of Manufacturer-Objects List<List<Car>> ...
309 votes
17 answers
254k views
What is the difference between map and flatMap and a good use case for each?
Can someone explain to me the difference between map and flatMap and what is a good use case for each? What does "flatten the results" mean? What is it good for?
252 votes
8 answers
183k views
What is the difference between Optional.flatMap and Optional.map?
What's the difference between these two methods: Optional.flatMap() and Optional.map()? An example would be appreciated.
96 votes
7 answers
147k views
Java 8 Streams FlatMap method example
I have been checking the upcoming Java update, namely: Java 8 or JDK 8. Yes, I am impatient, there's a lot of new stuff, but, there is something I don't understand, some simple code: final Stream<...
33 votes
3 answers
74k views
Collecting lists from an object list using Java 8 Stream API
I have a class like this public class Example { private List<Integer> ids; public getIds() { return this.ids; } } If I have a list of objects of this class like this List&...
22 votes
7 answers
9k views
How to find pairs of elements from two lists or arrays (cartesian product) using streams
I have two lists of numbers and I'd like to find all possible pairs of numbers. For example, given the lists [1, 2, 3] and [3, 4] the result should be: [(1, 3), (1, 4), (2, 3), (2, 4), (3, 3), (3, 4)] ...
21 votes
4 answers
34k views
How to use stream in Java 8 to collect a couple of fields into one list?
For example I have class Person with name and surname fields. I want to collect a List of String (names and surnames all together) from List of Person, but it seems that I can't use map twice per ...
23 votes
2 answers
19k views
Why is this usage of Stream::flatMap wrong?
I expected to be able to use Stream::flatMap like this public static List<String> duplicate(String s) { List<String> l = new ArrayList<String>(); l.add(s); l.add(s); ...
16 votes
2 answers
19k views
Java8: Stream map two properties in the same stream
I have a class Model with the following signature: class Model { private String stringA; private String stringB; public Model(String stringA, String stringB) { ...
13 votes
3 answers
4k views
Is flatMap guaranteed to be lazy? [duplicate]
Consider the following code: urls.stream() .flatMap(url -> fetchDataFromInternet(url).stream()) .filter(...) .findFirst() .get(); Will fetchDataFromInternet be called for second ...
6 votes
1 answer
7k views
Every combination of 2 strings in List Java 8 [duplicate]
I would like to combine every two strings in the list and return the list of combination using java8 streams: List<String> list; Stream.concat(list.stream(), list.stream()) ....
6 votes
2 answers
3k views
Passing objects through streams and flatmaps
I'm dealing with Java 8 streams and I wondering if I could resolve this problem in a fancy way. That's my scenario: Suppose I have a list of parties and inside each element I have the names of the ...
7 votes
1 answer
5k views
flatMap() to convert a stream of LinkedList<String> into a stream of String
My goal is exactly what the title say. What I'm doing is: .stream().flatMap(x -> x.getTitles()) getTitles() returns a LinkedList<String>, and I expected flatMap() to do the job and create ...
4 votes
2 answers
8k views
Java 8: Flattening Stream<Stream> [duplicate]
I want do a Cartesian product on Quarter on a year between the years specified Year(2105, 2016) should return Quarter(2015, Q1), Quarter(2015, Q2)... Quarter(2016, Q4) The enum representing Quarter ...