aspose file tools*
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "TreeMap question" Watch "TreeMap question" New topic
Author

TreeMap question

Jacob Sonia
Ranch Hand

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

The keys in the TreeMap are:
iu_1
pu_1
dbu_1
ru_1
iu_3

etc.
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.

Thanks
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
Thanks,
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: TreeMap question