util package
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Eg:
Set s=new HashSet();
s.add("S");
s.add("S");
Now how does s know that "S" is already there in the collection?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Do you know where to find the source code?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 ]
-
-
Number of slices to send:Optional 'thank-you' note:
-
-

-
-
Number of slices to send:Optional 'thank-you' note:
-
-
And dirk,what caused u to think that I dont know where the source code is?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
How did you define your equals method? And did you override the hashCode method as well?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.
Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
public boolean equals(Object a)
{
System.out.println("in my equals");
return super.equals(a);
}
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
| 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 |









