Adam Amin:
Tengo la siguiente lista de listas List<List<Integer>> dar = new ArrayList<List<Integer>>();
:
[[0], [1, 2, 0], [2, 1, 0], [5, 0]]
Quiero comprobar si el número entero 5 se encuentra en ninguna de las listas. Si uso if(dar.contains(5))
, volverá false
.
¿Qué puedo hacer yo para comprobar si existe un número entero en cualquiera de las listas?
GBlodgett:
Iterar sobre el List
de List
's y luego llamadas contiene:
for(List<Integer> list : dar) {
if(list.contains(5)) {
//...
}
}
O el uso Stream
s:
boolean five = dar.stream().flatMap(List::stream).anyMatch(e -> e == 5);
//Or:
boolean isFive = dar.stream().anyMatch(e -> e.contains(5));