This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Regarding Hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Regarding Hashmap" Watch "Regarding Hashmap" New topic
Author

Regarding Hashmap

santhosh kumar vk
Ranch Hand

Joined: Aug 25, 2009
Posts: 144
Hi,
Could you please tell me how to get the duplicate value from Hashmap.

Thanks,
Santhosh Kumar V.K
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Could you elaborate on your question?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
And is that even possible?
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

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

Joined: Oct 13, 2005
Posts: 37926
    
  22
Yes, but how are you going to find the duplicate values? I can think of a way, but it is longwinded.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7501
    
  18

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Shahir Deo
Ranch Hand

Joined: Mar 19, 2013
Posts: 78

Be specific with your question. Dont let others to form it.

you want return type as boolean? or Value itself


Knowledge enlivens the soul.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

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

Joined: Oct 13, 2005
Posts: 37926
    
  22
If you use a HashSet, the lookup is O(1).
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

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

Joined: Oct 13, 2005
Posts: 37926
    
  22
The principal time‑consuming step is calculating the hash code.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Aug 11, 2007
Posts: 4646
    
    5

We have, of course, completely lost the OP. She/He never came back to clarify our initial questions.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
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.
 
Don't get me started about those stupid light bulbs.
 
subject: Regarding Hashmap
 
Similar Threads
WA #1.....word association
Nested if's
Finding a Specific Object in an ArrayList
struts 2 s:radio tag
switch on String