Filter a list with condition on inner list

Yuri :

I have a list of objects. Each object contains another list. I want to filter the list with condition on inner list.

For example:

There is a list of factories. Each factory contains a list of different car models it produces. I want to filter factory list in such way that I will get only factories that produce Mazda3.

How can I do it with lambda?

It should be something similar to this:

factories.stream().filter(f -> f.getCars().stream().filter(c -> C.getName().equals("Mazda3")).).collect(Collectors.toList());
Eugene :

If I understood correctly(and simplified your example)

 List<List<Integer>> result = Arrays.asList(
                       Arrays.asList(7), 
                       Arrays.asList(1, 2, 3), 
                       Arrays.asList(1, 2, 4), 
                       Arrays.asList(1, 2, 5))
            .stream()
            .filter(inner -> inner.stream().anyMatch(x -> x == 5))
            .collect(Collectors.toList());

    System.out.println(result); // only the one that contains "5"[[1,2,5]] 

EDIT

After seeing your example you are looking for anyMatch

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=444207&siteId=1
Recommended