Good evening I would like you to help me in the following:
I have 2 ArrayList's:
ArrayList<Persona> listaPersona;
that its content transforming it into Json is the following:
[{"nombre":"Luis", "edad":"22"},{"nombre":"Juan","edad":"24"},
"nombre":"Jose","edad":"15"}]
Now, I have another ArrayList:
ArrayList<String> nombres=new ArrayList();
nombres.add("Luis");
nombres.add("Juan");
What can I do to remove from my "Personlist" the entire row that does not contain the name of my ArrayList "names"?
I would like my ArrayList<Persona>
to be as follows:
[{"nombre":"Jose","edad":"15"}]
I tried to do it in the following way:
for (int i=0; i<listaPersona.size(); i++) {
for (int j=0; j<nombres.size(); j++) {
if(!listaPersona.get(i).getNombre().equals(nombres.get(j))) {
listaPersona.remove(i);
i--;
}
}
}
But it is not the correct way since in the first iteration, Luis is compared with Juan and since they are not equal the row is eliminated, and that is what I do not want, I would greatly appreciate your help! Thanks in advance