• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TreeMap question

 
Jacob Sonia
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want you to know about how HashMap/TreeMap works? and you may require to know about hashing techniques!
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,
This looks good.
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic