• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Collection: Set overwrite or reject duplicate value ?

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
Rob
credits: original code from Marcus Green 1.4 Mock Question Bank ID 250,
modified with comments
 
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
setColl.add("SetElement"+i);
// 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).
setColl.add("SetElement"+i);
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();
while(it.hasNext()){
P.p(it.next());
}
}
public static void main(String[] args) {
SetExample obj = new SetExample();
obj.method();
}
}
-------
Let me know if you couldn't understand any thing in the code.
Thanks..
 
Rob Kiu
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Narasimha,
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.
Cheers,
Rob.
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.

 
Anything worth doing well is worth doing poorly first. Just look at this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic