Linked Questions

1 vote
2 answers
3k views

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.
kesari.j's user avatar
0 votes
1 answer
461 views

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>> ...
Philipp's user avatar
  • 480
309 votes
17 answers
254k views

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?
Eran Witkon's user avatar
  • 4,122
252 votes
8 answers
183k views

What's the difference between these two methods: Optional.flatMap() and Optional.map()? An example would be appreciated.
codependent's user avatar
  • 24.7k
96 votes
7 answers
147k views

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<...
chiperortiz's user avatar
  • 4,991
33 votes
3 answers
74k views

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&...
Jakob Nielsen's user avatar
22 votes
7 answers
9k views

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)] ...
Ankur Singh's user avatar
21 votes
4 answers
34k views

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 ...
user2620644's user avatar
23 votes
2 answers
19k views

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); ...
Simon's user avatar
  • 6,383
16 votes
2 answers
19k views

I have a class Model with the following signature: class Model { private String stringA; private String stringB; public Model(String stringA, String stringB) { ...
Sunflame's user avatar
  • 3,196
13 votes
3 answers
4k views

Consider the following code: urls.stream() .flatMap(url -> fetchDataFromInternet(url).stream()) .filter(...) .findFirst() .get(); Will fetchDataFromInternet be called for second ...
balki's user avatar
  • 27.9k
6 votes
1 answer
7k views

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()) ....
Lau's user avatar
  • 3,790
6 votes
2 answers
3k views

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 ...
FedericoAlvarez's user avatar
7 votes
1 answer
5k views

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 ...
RVKS's user avatar
  • 157
4 votes
2 answers
8k views

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 ...
Somasundaram Sekar's user avatar

15 30 50 per page