Reputation: 13666
Hi I have two ArrayList
which has HashMap
stored in it. For e.g
expectedList = [
{
id = 1, key = X, rowid = 1, id = 1, timeofday = 12: 12: 00,
stream = A
},
{
id = 999999999, key = Y, rowid = 2, id = 1, timeofday = 16: 12: 00,
stream = A
}]
tableList = [
{
id = 1, key = X, rowid = 1, id = 1, timeofday = 12: 12: 00,
stream = A
},
{
id = 999999999, key = Y, rowid = 2, id = 1, timeofday = 16: 12: 00,
stream = A
}]
expectedFileList.equals(tableList)
//returns false any idea why? Data is exactly same in both lists even order is same still it returns false. Please guide. Thanks in advance.
Upvotes: 0
Views: 1706
Reputation: 40335
Two things:
The last element in tableList says bundlestream=...
, while expectedList just says stream=...
. Something is clearly different. Edit: It appears the OP edited out this change; so it must have been a typo, which leaves:
Are you sure the objects stored in the list implement equals() properly (or, in this case, the keys and values in the HashMaps in the lists)? If those objects' equals() are not returning expected results, then neither will the ArrayList's equals().
Temporary Edit:
@OP: Can you run the following code:
public static <T,U> void dumpList (List<HashMap<T,U>> list) {
System.out.println("List:");
for (HashMap<T,U> map:list)
for (Map.Entry<T,U> e:map.entrySet())
System.out.println(e.getKey().getClass() + ", " + e.getValue().getClass());
}
On both of your lists, e.g.:
dumpList(tableList);
dumpList(expectedList);
And post the output that it prints in your question?
Upvotes: 4
Reputation: 726
First you have to understand how equals works in ArrayList
.
And also you stored HashMap
with in Array.
This is how equals method works in ArrayList
public boolean equals(Object paramObject)
{
if (paramObject == this)
return true;
if (!(paramObject instanceof List))
{
return false;
}
ListIterator localListIterator1 = listIterator();
ListIterator localListIterator2 = ((List) paramObject).listIterator();
while ((localListIterator1.hasNext()) && (localListIterator2.hasNext()))
{
Object localObject1 = localListIterator1.next();
Object localObject2 = localListIterator2.next();
if (localObject1 == null)
if (localObject2 != null)
;
else if (!(localObject1.equals(localObject2)))
return false;
}
return ((!(localListIterator1.hasNext())) && (!(localListIterator2.hasNext())));
}
Yes, ArrayList internally compare each and evertObject in List, the compare by means of again, equals
method of element in ArrayList.
In your case the element is HashMap, so U also need to know how equals
method works in HashMap. http://javarevisited.blogspot.in/2011/02/how-hashmap-works-in-java.html
So, the sort and quick solution is that just overide equals
method of your own. And dont forget to implement hasCode
method also.
How to return index of ArrayList<Field>
Sorry for the long Answer.
Upvotes: 0
Reputation: 14699
equals()
for ArrayList
is inherited from AbstractList
:
public boolean equals(Object o)
Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order.
So, perhaps you have not overridden .equals()
for at least one of the objects in your List
.
Upvotes: 0
Reputation: 11950
The problem is that the last object of data is different in them.
stream=A
and bundlestream=A
Upvotes: 0