목록 목록의 요소를 찾는 방법

아담 아민 :

나는 목록의 다음 목록을 가지고 List<List<Integer>> dar = new ArrayList<List<Integer>>();:

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

나는 정수 5 목록 중 하나에 있는지 여부를 확인합니다. 내가 사용하는 경우 if(dar.contains(5)), 그것은 반환합니다 false.

내가 정수는리스트의 존재 여부를 확인하기 위해 무엇을 할 수 있는가?

GBlodgett :

오버으로 반복 ListList의 다음 호출이 포함되어 있습니다 :

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

또는 사용 Stream의 :

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

추천

출처http://43.154.161.224:23101/article/api/json?id=314892&siteId=1