Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Java in General
Search Coderanch
Advance search
Google search
Register / Login
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
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Liutauras Vilda
Paul Clapham
Sheriffs:
paul wheaton
Tim Cooke
Henry Wong
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Piet Souris
Bartenders:
Mike London
Forum:
Java in General
Overriding equals() & hashcode() in collections
ramya narayanan
Ranch Hand
Posts: 338
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Dear all,
What is the need for overriding equals() & hashcode() in Collections .
Normally every instance by default inherits Object class which has equals() & hashcode() . Then why we should override it?
Regards.
Campbell Ritchie
Marshal
Posts: 77217
370
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
public class Name { private final String name; public Name(String name) { this.name = name; } public String getName() { return name; } /** * This method calls "super" and performs exactly the same as the Object class * method of the same name. */ @Override public boolean equals(Object other) { return super.equals(other); } } public class NameDemo { /** * Two command-line arguments required. */ public static void main(String[] args) { Name name1 = new Name(args[0]); Name name2 = new Name(args[1]); System.out.printf("Name %s == Name %s: %b%n%n", name1.getName(), name2.getName(), name1.equals(name2)); } }
java
NameDemo ramya ramya
See what comes out. You need to have two identical objects recoognised as identical to each other for the Collections to work.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
HashCodes
Regarding equals method
HashCode Call
equals() and hashCode() required to override for class used in Map key
hashcode
More...