We Are Going To Discuss About Nested lists with streams in Java8. So lets Start this Java Article.
Nested lists with streams in Java8
- Nested lists with streams in Java8
You can do it with
flatMap
.
I made an example withCompany
which contains a list ofPerson
:public static void main(String[] args) { List<Company> companies = Arrays.asList( new Company(Arrays.asList(new Person("Jon Skeet"), new Person("Linus Torvalds"))), new Company(Arrays.asList(new Person("Dennis Ritchie"), new Person("Bjarne
- Nested lists with streams in Java8
You can do it with
flatMap
.
I made an example withCompany
which contains a list ofPerson
:public static void main(String[] args) { List<Company> companies = Arrays.asList( new Company(Arrays.asList(new Person("Jon Skeet"), new Person("Linus Torvalds"))), new Company(Arrays.asList(new Person("Dennis Ritchie"), new Person("Bjarne
Solution 1
You can use two flatMap
then a filter
then you can pick the first one or if no result return null
:
C c1 = listOfAObjects.stream()
.flatMap(a -> a.getList().stream())
.flatMap(b -> b.getPr().stream())
.filter(c -> c.getName().equalsIgnoreCase(name))
.findFirst()
.orElse(null);
Original Author YCF_L Of This Content
Solution 2
You can do it with flatMap
.
I made an example with Company
which contains a list of Person
:
public static void main(String[] args) {
List<Company> companies = Arrays.asList(
new Company(Arrays.asList(new Person("Jon Skeet"), new Person("Linus Torvalds"))),
new Company(Arrays.asList(new Person("Dennis Ritchie"), new Person("Bjarne Stroustrup"))),
new Company(Arrays.asList(new Person("James Gosling"), new Person("Patrick Naughton")))
);
List<String> persons = companies.stream()
.flatMap(company -> company.getPersons().stream())
.map(Person::getName)
.collect(Collectors.toList());
System.out.println(persons);
}
Output :
[Jon Skeet, Linus Torvalds, Dennis Ritchie, Bjarne Stroustrup, James Gosling, Patrick Naughton]
Original Author Arnaud Denoyelle Of This Content
Solution 3
I had same task but i had one nested class. And I had to filter the objects with filter in the nested collection.
As a result, I had to get items that have matches in the collection.
for example:
public class RootElement {
private String name;
private List<String> nestedElements;
//getters / setters and constructors
}
init collection with elements:
List<RootElement> elements = Arrays.asList(
new RootElement("first", Arrays.asList("one", "two", "three")),
new RootElement("second", Arrays.asList("four", "one", "two")));
filter example:
String filterParam = "four";
List<RootElement> filtered = elements.stream()
.flatMap(root -> root.getNestedElements()
.stream()
.filter(nested -> nested.equalsIgnoreCase(filterParam))
.map(filteredElement -> new RootElement(root.getName(), root.getNestedElement())))
.collect(Collectors.toList());
Hope it will help someone.
Original Author Alexander Yushko Of This Content
Solution 4
listOfObjectsA.stream()
.flatMap(a -> a.getListOfObjectsB.stream())
.flatMap(b -> b.getListOfObjectsC().stream())
.filter(c -> name.equals(c.getName()))
.findAny()
.orElse(....)
Original Author Eugene Of This Content
Conclusion
So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.