• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

how can we get all the keys having same value in TreeMap

 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I want all the keys which has the same value in TreeMap. How can I get that.Please help me.
Thanks in advance.
Regards,
Divya Gehlot
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know of a better way than iterating over all entries and doing it manually.
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no direct method to get that.

However, you can do that in the following way:
  • Create a new Map instance that has the key as the values in the original map and value as the list of keys.
  • Get entrySet from the original map
  • Iterate over the entry set, adding value to the key of the new map and key to the value list.
  • At the end of this iteration, you will get all the keys that have the same values.


  • The above will be the easiest way of doing it and there may be better algorithms to do the same.
    Also, if this is the predominant work on the original map then you must think of using a different data structure.
     
    Nitesh Kant
    Bartender
    Posts: 1638
    IntelliJ IDE MySQL Database Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Ilja Preuss:
    I don't know of a better way than iterating over all entries and doing it manually.


    Oh yeah, i dont think there is an alternative.
    By the way, this does not sound like an advanced question.
     
    Ilja Preuss
    author
    Posts: 14112
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Nitesh Kant:
    However, you can do that in the following way:

  • [list]Create a new Map instance that has the key as the values in the original map and value as the list of keys.



  • You might want to use a MultiHashMap for this: http://commons.apache.org/collections/apidocs/org/apache/commons/collections/MultiHashMap.html
     
    Politics is a circus designed to distract you from what is really going on. So is this tiny ad:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic