wood burning stoves 2.0
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Collection: Set overwrite or reject duplicate value ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Collection: Set overwrite or reject duplicate value ?" Watch "Collection: Set overwrite or reject duplicate value ?" New topic

Collection: Set overwrite or reject duplicate value ?

Rob Kiu

Joined: Feb 02, 2004
Posts: 15
Hi All,
What is the behaviour of Set when dealing with duplicate values, eg we use method add("One") then add("One") to say a HashSet, does it overwrite the earlier "One" with latest value of "One" or reject the latest "One"?
For comparison, below code shows for Map which does not allow duplicate key, the behavior is overwrite the earlier key, so output is Two for all Map classes.
I am not sure how to write code for Set to verify its behavior. Anyone can help with some code sample for Set similar to verify Map?
Thanks very much,
credits: original code from Marcus Green 1.4 Mock Question Bank ID 250,
modified with comments
Narasimha Rao B.
Ranch Hand

Joined: Aug 26, 2002
Posts: 205
If you try to add duplicate values to the Set, false will be returned.
When ever you add any object to the Set, boolean value will be returned. True will be returned, if it adds successfully and False will be returned, if it could not add the element. So if you are adding the duplicate elements, it will not add and false will be returned.
You can practice my code, which is attached at the bottom.
Comment the line, //(1) and you can see false values, try to add here some distinguish values(Say your name, "Rob Kiu"), you will see 'true' in the output.
See the code.......
import java.util.*;
public class SetExample {
public void method(){
// Here i am creating the HastSet object, you can create other Set objects too.
HashSet setColl;
setColl=new HashSet();
for (int i=0;i<4;i++){
// Here i am adding the elements to the set.
// Here i am adding the duplicate entries. But those will not be stored and all the duplicate entries will
// be regected. And boolean will be retruned. To see the same uncomment the below second line, //(1).
System.out.println(setColl.add("SetElement"+i)); // (1)
// I am creating the Iterator, so that i can loop through the iterator and can access the elements of the set class.
Iterator it = setColl.iterator();
public static void main(String[] args) {
SetExample obj = new SetExample();
Let me know if you couldn't understand any thing in the code.

Rob Kiu

Joined: Feb 02, 2004
Posts: 15
You code is clear to me. Thanks very much ! Didn't realise result of method add(element) is a boolean and thus can be used to check.
So now I understand & confirm for myself: Set will neglect (or some mock exam use the term reject) duplicate elements while Map will overwrite duplicate keys with latest one.
p.s. Narasimha, hope u don't mind, modify your codes below to form more easily understood by me...hope it'll be of help 2 others as well.

I agree. Here's the link: http://aspose.com/file-tools
subject: Collection: Set overwrite or reject duplicate value ?
jQuery in Action, 3rd edition