Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

util package

 
Gautam Sewani
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • 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?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • 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?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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?
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • 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
  • 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);
}
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic