This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes TreeMap question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "TreeMap question" Watch "TreeMap question" New topic

TreeMap question

Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 174
I have a TreeMap

The keys in the TreeMap are:

The number index is not fixed and can change.

So, i want to know if my TreeMap contains more than one value with same number index, like iu_1 and pu_1 have the same index, if so i want to add some error codes.

One way to find this is to iterate the whole map and then check for each key's number index and see if it is equal to the previous one.

But is there a faster and easier way to do it. Please help me with suggestions.

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

I want you to know about how HashMap/TreeMap works? and you may require to know about hashing techniques!
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

What you are trying to do is quite unusual, so there's nothing built-in to do this. I'd do something like
Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 174
This looks good.
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

Incidentally I was playing aroud with this earlier. If you learn Scala, you can do fun stuff like to get a map of the duplicated indicies to the keys containing them.

E.g. for the above example the string value of duplicates is Map(2 -> Set(dbu_2, iu_2)) (only 1 element, but if there were more duplicates there would be more).

Scala might look a bit cryptic but it's pretty simple once you know that _ is the wildcard, standing for each element of the collection.
It is sorta covered in the JavaRanch Style Guide.
subject: TreeMap question
Similar Threads
Hashtable iterator question
how to order numbers?
need gud solution
Code optimization and good practices