wish helps you By using flatMap you can achieve this, First stream the stores list and then in flatMap stream List by mapping each DayOfWeek to OtherType.
//Assuming you have constructor in OtherType with three arguments like OtherType(String storeId, String storeName, DayOfWeek day)
List<OtherType> transformed = stores.stream()
help you fix your problem Type inference is a new feature. Until tools and IDEs are fully developed I recommend using explicitly typed lambdas. There ware cases where Eclipse even crashed if an explicit cast was missing, but that is fixed now. Here's a workaround:
it fixes the issue I'm trying to understand flatMap: flatMap(x->stream.of(x) ) does not flat the stream and flatMap(x->x.stream()) works and gives the desired result. Can someone explain the difference between two? , The thing you had in mind was this:
// String::new isn't really needed in this simple example,
// but would be in a more complex one...
Java 8 stream - Referring to original stream object after flatmap
I wish did fix the issue. It’s hard to be sure from your code snippets where we don’t know the types of the variables you use nor the return types of the methods. But I think that the source of your error is that you are trying to pass a lambda that returns a Stream to Optional.flatMap(). Let’s look at Stream first. Stream.flatMap() takes a function that returns a Stream. This seems to be exactly what you are giving it in your first code snippet.
I hope this helps . I was wondering how I could use Java Stream API to flatten a structure where I have an object and a collection of the same type objects nested in it. , You can use flatMap with Stream concatenation: