• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Regarding Hashmap

 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Could you please tell me how to get the duplicate value from Hashmap.

Thanks,
Santhosh Kumar V.K
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you elaborate on your question?
 
Marshal
Posts: 73970
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And is that even possible?
 
Rancher
Posts: 4686
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 73970
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 80
Hibernate Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4686
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 73970
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you use a HashSet, the lookup is O(1).
 
Pat Farrell
Rancher
Posts: 4686
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 73970
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The principal time‑consuming step is calculating the hash code.
 
Pat Farrell
Rancher
Posts: 4686
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4686
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 73970
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic