Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes util package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "util package" Watch "util package" New topic
Author

util package

Gautam Sewani
Ranch Hand

Joined: Apr 19, 2002
Posts: 93
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

Joined: Dec 10, 2001
Posts: 7023
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?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Dec 10, 2001
Posts: 7023
Karl, really... tsk, tsk, tsk... SCJP perhaps, but certainly not JavaRanch UBB Certified!
Gautam Sewani
Ranch Hand

Joined: Apr 19, 2002
Posts: 93
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

Joined: Dec 10, 2001
Posts: 7023
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

Joined: May 05, 2000
Posts: 13974
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?


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Gautam Sewani
Ranch Hand

Joined: Apr 19, 2002
Posts: 93
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

Joined: Jul 03, 2001
Posts: 2120
Gautam
Is your class extending Object?
Have you overriden hashCode to return the same int for those instances considered the same by equals?


SCJP2. Please Indent your code using UBB Code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: util package