How can I perform a dirty check on an ArrayList
in Java?
I have an object
Animal
{
String animalName;
String animalType;
String animalPlace
}
I have two lists:
List<Animal> animalList1 = new ArrayList<Animal>();
List<Animal> animalList2 = new ArrayList<Animal>();
In the first list, I have the values:
[0][lion,Carnivorous,Africa]
[1][tiger,Carnivorous,Zambia]
[2][Goat,Herbivorous,Zimbabwe]
In the second list, I have the values:
[0][lion,Carnivorous,Africa]
[1][tiger,Carnivorous,Zambia]
[2][Goat,Herbivorous,Norway]
In the second list,
the animalPlace
for the element at index 2 has changed from Zimbabwe to Norway.
How do I compare the two list values and the values which have changed put that object in a separate list?
List<Animal> animalList3 = new ArrayList<Animal>();
[0] [Goat,Herbivorous,Norway]
I know the traditional way of doing it by comparing the list using a for loop:
for (int i = 0; i < list1.size(); i++)
{
for (int j = i+1; j < list2.size(); j++)
{
// compare list1 and list2 values
}
}
Is there any faster and/or better way?