| Author |
Adding duplicate values into HashSet
|
Harshal Gurav
Ranch Hand
Joined: May 29, 2008
Posts: 150
|
|
Hi,
we know that,HashSet does not allows duplicate values .But when we try to insert duplicate values into hashSet,it does not give any compile or run time error ?
What is exactly means by
HashSet does not permits duplicate value
suppose we have this code
When we complile and run this programme, it will give output as [1,2]
Thanks
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32652
|
|
Where did you get that quote from? It looks misleading.
HashMap does not permit duplicate keys. If you have two values with the same associated key, it retains which ever was "put" more recently, and loses the older value.
You cannot have a HashMap<Integer>; it needs two types. It has no add() method.You need to read the Java™ Tutorials section, particularly about Map.
|
 |
Harshal Gurav
Ranch Hand
Joined: May 29, 2008
Posts: 150
|
|
Sorry for previous post.
All this is related to HashSet.
Yes, In HashMap, we have value is associated with Key.
How it is work with HashSet.
|
 |
Martin Vanyavchich
Ranch Hand
Joined: Sep 16, 2008
Posts: 241
|
|
|
Harshal Gurav, are you were reffering to a HashSet? If so, note that add() method returns a boolean value of true, if insertion was successful.
|
SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
|
 |
Steve Luke
Bartender
Joined: Jan 28, 2003
Posts: 3030
|
|
As Campbell said, you are confused somewhere with your code. A HashMap can have multiple duplicate values, just not duplicate keys. A HashMap has to be declared with two generic types, one for the key and one for the value. And there is no add(value) method in a HashMap. Assuming you meant no duplicate keys, the generics stuff was a mistake, and you meant put(key, value), then the API documentation tells you what will happen:
HashMap#put(key, value)
Or you could mean a HashSet. A HashSet allows no duplicate values, has a single generic attribute and does have an add(value) method. Here, too, the API explains what happens when you try to add duplicates:
HashSet#add(value)
|
Steve
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32652
|
|
No, it's me getting confused. Sorry. I read "Set" as "Map".
|
 |
Harshal Gurav
Ranch Hand
Joined: May 29, 2008
Posts: 150
|
|
Thanks for reply.
If HashSet does not give error while adding duplicate,then what is exactly use of Hashset?
Is add() method is used for any comparison purpose?
|
 |
Martin Vanyavchich
Ranch Hand
Joined: Sep 16, 2008
Posts: 241
|
|
Campbell Ritchie wrote:No, it's me getting confused. Sorry. I read "Set" as "Map".
I think you've read it just fine before it was edited
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32652
|
|
It was edited, was it?
Harshal Gurav, don't make that sort of change which makes subsequent posts look like nonsense
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32652
|
|
|
The java.util.Set interface is designed to operate like the mathematical abstraction of a set. You will have to learn a little basic set theory, then you will understand why add() doesn't insert the same value twice.
|
 |
satish varma
Greenhorn
Joined: Feb 11, 2010
Posts: 27
|
|
we know that, HashSet does not allows duplicate values .But when we try to insert duplicate values into hashSet,it does not give any compile or run time error ?
What is exactly means by
"HashSet" duplicate values are not allowed "if we are trying to add duplicate objects into HashSet there is no compile time and run time error simply add() returns false. thats it"
check out below code
|
 |
 |
|
|
subject: Adding duplicate values into HashSet
|
|
|