How to find an element in a list of lists

Adam Amin :

I have the following list of lists List<List<Integer>> dar = new ArrayList<List<Integer>>();:

[[0], [1, 2, 0], [2, 1, 0], [5, 0]]

I want to check whether the integer 5 is in any of the lists. If I use if(dar.contains(5)), it will return false.

What can I do to check whether an integer exists in any of the lists?

GBlodgett :

Iterate over the List of List's and then call contains:

for(List<Integer> list : dar) {
    if(list.contains(5)) {
       //...
    }
}

Or use Streams:

boolean five = dar.stream().flatMap(List::stream).anyMatch(e -> e == 5);
//Or:
boolean isFive = dar.stream().anyMatch(e -> e.contains(5));

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=308530&siteId=1