Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within OCPJP
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
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Programmer Certification (OCPJP)
K&B Chapter 7 Question # 12
Paras Jain
Ranch Hand
Posts: 141
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I have fixed the code as suggested by the question and implemented the equals method as follows
public class Group extends HashSet<Person> { public static void main(String[] args) { Group g = new Group(); g.add(new Person("Hans")); g.add(new Person("Lotte")); g.add(new Person("Jane")); g.add(new Person("Hans")); g.add(new Person("Jane")); System.out.println("Total: " + g.size()); } public boolean add(Person p) { System.out.println("Adding: " + p); return super.add(p); } } class Person{ private final String name; public Person(String name) { this.name = name; } public String toString() { return name; } public boolean equals(Object o){ return name.equals(((Person)o).name); } }
Even now I am getting 5 as the answer. Please tell me what is wrong
Paras Jain
SCJP 5.0
Paras Jain
Ranch Hand
Posts: 141
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I got this corrected by implementing hashCode() method. I have referred to one of the questions asked earlier in the forum.
Paras Jain
SCJP 5.0
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Another Generics question
A Generics and Collections Problem
what is the correct answer?
Correctly override equals and hashcode
More...