• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Hashmap

 
santhosh kumar vk
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Could you please tell me how to get the duplicate value from Hashmap.

Thanks,
Santhosh Kumar V.K
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you elaborate on your question?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49808
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And is that even possible?
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean the standard Java class: "Class HashMap<K,V>"

Its easy to have duplicate values. Its impossible to have duplicate keys.


 
Campbell Ritchie
Sheriff
Pie
Posts: 49808
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but how are you going to find the duplicate values? I can think of a way, but it is longwinded.
 
Winston Gutkowski
Bartender
Pie
Posts: 10508
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
santhosh kumar vk wrote:Could you please tell me how to get the duplicate value from Hashmap.

What duplicate value? As already said, if you're talking about two values with a duplicate key, it can't be done without making each "value" a list or array.

Also: I hate to say, Santhosh, but you really should get better at asking specific questions, rather than just machine-guinning whatever's at the top of your head. It's a very bad way to learn.

Winston
 
Shahir Deo
Ranch Hand
Posts: 80
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be specific with your question. Dont let others to form it.

you want return type as boolean? or Value itself
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Yes, but how are you going to find the duplicate values? I can think of a way, but it is longwinded.

use a FOR loop to transfer the values into a SET. If the set grows, its not a dup. If the set size stays the same, its a dup. Put dups in a list.

From the application programmer's view, its O(N), but the set lookup is probably really O(N ln N).
At least the code is small.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49808
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use a HashSet, the lookup is O(1).
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If you use a HashSet, the lookup is O(1).

Of course. But we don't know the constants.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49808
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The principal time‑consuming step is calculating the hash code.
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The principal time‑consuming step is calculating the hash code.

If the hash has good distribution. If there are a lot of hash bucket duplicates, the usual implementation has a linked list of key values. Searching that list is always O(N/2) === O(N)
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Farrell wrote:If the hash has good distribution. If there are a lot of hash bucket duplicates, the usual implementation has a linked list of key values. Searching that list is always O(N/2) === O(N)


So, yes, if you write your code correctly, it will behave well, and if you don't, it will behave poorly. When someone says HashMap is O(1), I've always assumed they were talking about a properly written HashMap implementation storing objects with a decent hashCode() implementation, as would most people, I expect.
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have, of course, completely lost the OP. She/He never came back to clarify our initial questions.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49808
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote: . . . I've always assumed they were talking about a properly written HashMap implementation storing objects with a decent hashCode() implementation, as would most people, I expect.
Agree. I was taking that for granted.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic