• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

TreeMap question

 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want you to know about how HashMap/TreeMap works? and you may require to know about hashing techniques!
 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks,
This looks good.
 
Luigi Plinge
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic