0

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.

6
  • can you indent please Commented Aug 8, 2013 at 4:25
  • 2
    Is equals defined for the stored objects? If not that could be your problem. Commented Aug 8, 2013 at 4:25
  • I am sorry all that was typo both list has stream and not bundlesteam. Commented Aug 8, 2013 at 4:31
  • How can each HashMap contain two entries with the key "id"? Every key in a HashMap should be unique, and HashMap.put(K,V) replaces an existing entry if you use an existing key. Commented Aug 8, 2013 at 4:38
  • Doesn't looks like Java, more like JSON. Commented Aug 8, 2013 at 4:43

6 Answers 6

4

Two things:

  1. 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:

  2. 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?

Sign up to request clarification or add additional context in comments.

4 Comments

Hi thanks for the input. I am sorry all that was typo both list has stream and not bundlesteam. Inside object are Hashmap and inside hashmap most of the things are String.
Every key and value in the HashMaps must have equals() properly implemented. When you say "most" of the things are Strings, that implies some of them are not. Are you sure all of your key and value types have equals() properly implemented?
See my temporary edit above. Would you mind running that function on your lists and posting the results for each list up in your question? That will print out the class names of all the items in the lists.
Hi @Jason you were right it worked thanks a lot there was one data type which was not String
1

Data is different

stream=A
vs
bundlestream=A

Comments

0

Your tableList has bundlestream=A

Comments

0

The problem is that the last object of data is different in them.

stream=A and bundlestream=A

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.