• 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:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

util package

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How does an implementation of the Set interface know that an object is already there in its collection and its duplicate shall not be added?
Eg:
Set s=new HashSet();
s.add("S");
s.add("S");
Now how does s know that "S" is already there in the collection?
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at the source code for the class HashSet (and HashMap) and you'll find the answer.
Do you know where to find the source code?
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The javadoc for Set.add() says:


public boolean add(Object o)
Adds the specified element to this set if it is not already present (optional operation). More formally, adds the specified element, o, to this set if this set contains no element e such that (o==null ? e==null : o.equals(e)). If this set already contains the specified element, the call leaves this set unchanged and returns false. In combination with the restriction on constructors, this ensures that sets never contain duplicate elements.


So I think, an implementation of Set.add()
have to call equals() for each object in the
collection.
Hope this helps
[ Changed CODE tags to QUOTE tags to help with spacing ]
[ May 08, 2002: Message edited by: Jessica Sant ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Karl, really... tsk, tsk, tsk... SCJP perhaps, but certainly not JavaRanch UBB Certified!
 
Gautam Sewani
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont know wether karl is right,coz when I defined my own equals method in my class and added it to hashset,than the equals method was not called.
And dirk,what caused u to think that I dont know where the source code is?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sir Gautam,
I did not know whether you knew how to find the source files. You had not mentioned whether you had consulted and understood them.
So, did you find your answer in the source files?
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gautam Sewani:
I dont know wether karl is right,coz when I defined my own equals method in my class and added it to hashset,than the equals method was not called.

How did you define your equals method? And did you override the hashCode method as well?
 
Gautam Sewani
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I defined the equals method as:
public boolean equals(Object a)
{
System.out.println("in my equals");
return super.equals(a);
}
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gautam
Is your class extending Object?
Have you overriden hashCode to return the same int for those instances considered the same by equals?
 
No matter. Try again. Fail again. Fail better. This time, do it with this tiny ad:
Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders
https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing
reply
    Bookmark Topic Watch Topic
  • New Topic