• Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding duplicate values into HashSet  RSS feed

 
Harshal Gurav
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it's me getting confused. Sorry. I read "Set" as "Map".
 
Harshal Gurav
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It was edited, was it?
Harshal Gurav, don't make that sort of change which makes subsequent posts look like nonsense
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!