2
Predicate
is a functional interface that allows to test objects of a given
type. Given a Predicate
, the removeIf
will remove all elements
that return true to that predicate.
When trying to use the method removeIf()
, to test on a list that receives a Predicate, I’m having the exception quoted in the title, someone knows why ?
Code:
Predicate<Usuario> predicado = new Predicate<Usuario>() {
public boolean test(Usuario u) {
return u.getPontos() > 160;
}
};
List<Usuario> lista = Arrays.asList(usuario,usuario1, usuario2, usuario3);
lista.removeIf(predicado);
lista.forEach(u -> System.out.println(u));
But when I do so it print without error:
Predicate<Usuario> predicado = new Predicate<Usuario>() {
public boolean test(Usuario u) {
return u.getPontos() > 160;
}
};
List<Usuario> lista = new ArrayList<Usuario>();
lista.add(usuario2);
lista.add(usuario3);
lista.add(usuario1);
lista.add(usuario);
lista.removeIf(predicado);
lista.forEach(u -> System.out.println(u));
Does anyone know why of the exception thrown, when I pass the users inside the :
Arrays.asList();
In the case Arrays.asList is imutavel ? and Arraylist<>() not ?
– HashMap
It is not immutable, but cannot change size (you can change the value of the elements).
ArrayList
is aList
"normal", which allows removal normally.– Pablo Almeida
Thanks understand man, thanks for the force!
– HashMap