In a set if we have 5 objects {'a','v','d','w','r'}
And if i add one more same element say 'd', will the earlier 'd' object be retained in Set or it will be replaced by new 'd' object.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
In a set if we have 5 objects {'a','v','d','w','r'}
And if i add one more same element say 'd', will the earlier 'd' object be retained in Set or it will be replaced by new 'd' object.
Thread moved to http://www.javaprogrammingforums.com...ions-generics/
Did you read the API on Set? To quote the doc on the add method
If this set already contains the element, the call leaves the set unchanged and returns false
Hello tcstcs!
The javadoc for Set.add(E e) says;
"Adds the specified element to this set if it is not already present (optional operation). More formally, adds the specified element e to this set if the set contains no element e2 such that (e==null ? e2==null : e.equals(e2)). If this set already contains the element, the call leaves the set unchanged and returns false. In combination with the restriction on constructors, this ensures that sets never contain duplicate elements."
From what I get from the above, I would answer to your question that the new 'd' will never be added to the set, therefore the earlier 'd' will be retained.
You could also take a look at an interesting discussion about duplicates in sets that I found.
Hope it helps.
HashSet is implemented with HashMap. So in HashMap, if i add element with existing key the older value gets replaced with new one. Then in set as its based on HashMap, older value should be replaced with new one rite?
Write a small test program and see what happens.should be replaced
If you don't understand my answer, don't ignore it, ask a question.
with the program, we will not be able to see the results. Reason being
If set has {a,s,d,f}
I will add 's' to set and after addition it will be {a,s,d,f}
So we will not know whether its prev 's' or new 's'.
By comparing the references to the obects using the == operator.
If you don't understand my answer, don't ignore it, ask a question.
I wrote this program but its giving o/p as false,false only without duplicates.
public static void main(String[] args)
{
HashSet hh = new HashSet();
String aa = new String("a");
String bb = new String("b");
hh.add(aa);
hh.add(bb);
Iterator it = hh.iterator();
while(it.hasNext())
{
System.out.println("---"+aa==it.next());
}
}
What is printed out by your program? Is that what you expected to see?
If you don't understand my answer, don't ignore it, ask a question.
IF you make a set of with Set<object> declaration u need to make sure that Class(for object) overrides the hash codes and equals method correctly else duplicates will be allowed. when you are working with Set<String> it will not allow you to add "A" or "D" two times as the String class has hashcode and equals method implemented.