File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes How to check duplicates in hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to check duplicates in hashmap" Watch "How to check duplicates in hashmap" New topic

How to check duplicates in hashmap

vin Hari
Ranch Hand

Joined: Nov 16, 2006
Posts: 161
Hello all,
Iam using multimap to store duplicates ,for each key multiple values,
but how to retrive it means how to check for duplicate values while retrieving.please tell me how to check duplicate values.
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

The keys for a HashMap are stored in a Set - it won't allow duplicates. Either way, you can use the containsKey or containsValue method to look for anexisting record before insertion.
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
This is an intermediate question, probably, but strikes me as belonging in advanced because complexity is involved.

MultiMap maps a key to a List or Vector which would mean that you would call get Iterator on the the return from a key lookup. You would then have to walk the value with the iterator, counting and testing and so on. I don't see where an Iterator is going to give out a count(), accessing the list or vector before walking the iterator would tell how many elements are in the List or Vector

"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
vin Hari
Ranch Hand

Joined: Nov 16, 2006
Posts: 161
Thank you,
For reply, but iam not checking for duplicates while inserting,Actually what iam trying is iam creating menu driven program for which it requires for single submenu can contain many many elements thats why iam using multi map to insert but while retriving there values i want to check if next key is similar to previous key then make it as submenu and take out only values which form a items of submenu for that i require to see for duplicates even i tried with hashcode() but i did not get,
any more solutions please.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Can you show us a little code that almost works? Then we can tell exactly where you're stuck.

Is there something like this?

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: How to check duplicates in hashmap
It's not a secret anymore!