Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

how does equals() work with lists as objects?  RSS feed

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



output:
false
true
false
false

In the program above, list1 and list2 are compared using equals() in line 1 and line 2.
unless we override the equals() for comparing object references, equals() always returns false. As per the explanation equals() returns "false" in line 1 and line 3. But it returns "true" in line 2?
so does LinkedList or any Collection, overrides the equals() method of object?
Is the equals() method overridden by List, Set, Map ?
As line 4 returns false, equals() is not overridden by PriorityQueue?
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only Set, List and Map interfaces have a requirement to implement equals method in an elements-based way. In case of Collection it's not necessary and in a Queue it's even discouraged. That's what API for these types says.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sets, Lists, and Maps do not necessarily override equals. LinkedList does because it is a subclass of AbstractList which overrides equals. However, we can not say anything in general about whether a implementation of a Collection does or does not override equals. You will have to check the javadoc for the particular implementation you are working with.

unless we override the equals() for comparing object references, equals() always returns false


This is not true. If you don't override equals, it will return true if and only if the two objects references point to the same object (the same address in memory).
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please refer API http://java.sun.com/j2se/1.4.2/docs/api/java/util/Vector.html#equals(java.lang.Object)
It states that "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. In other words, two Lists are defined to be equal if they contain the same elements in the same order."
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println(p1.equals(p2));

Should this not return true?
 
Mazer Lao Tzu
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sreenivasa - Be careful. That statement applies to the Vector equals method, not all list-based equals methods.

Rajesh - No, will not return true because:
  • p1 is a PriorityQueue
  • p1 and p2 are different objects in memory (they have different memory addresses)
  • PriorityQueue does not override the equals method from Object.
  •  
    Sreenivasa Rao Venepalli
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Mazer,
    I am sorry if I mislead you. I thought all implementing class of List interface supports above statement. is it not true?
     
    Sheriff
    Posts: 9613
    37
    Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sreenivasa, you are right, the new documentation of List interface defines the behavior of equals method implemented by all List implementation more clearly

    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. This definition ensures that the equals method works properly across different implementations of the List interface.


    So if you try this code, you'll get true as the answer
     
    Mazer Lao Tzu
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Apparently I was wrong. Sorry about that. Thank you Ankit for correcting me.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!