• 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

hashMap question

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a hashmap set containing ::
<Key,value>
<3,a>
<5,s>
<1,r>
<0,y>
<2,k>
<4,l>

I want a result like:
<Key,value>
<3,a,y,r,k>
<5,s,y,r,k,a,l>
<1,r,y>
<0,y>
<2,k,y,r>
<4,la,y,r,k>

i.e everything is incremental....here values taken are string(to show)....but can be any objects
for example we take 0 since all other key are greater than it , hence no values except its own are added to it...
and in case of 5 (being the highest), all the values are added including its own.

how to accomplish this task using HashMap??
i am new to java and hashmap set.....can someone help me!!







 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to know why you need to have this first ?

You can use collections nesting each other to achieve anything like this.
Here you can use a List inside the Map, where you keep adding the items to the list on every increment.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might be able to save yourself some storage by creating a TreeMap which puts all the Keys in order. Then using treeMap.headMap(endingKey) or treeMap.tailMap(startingKey) to generate views of the map which contains the key you want plus all subsequent key/value pairs. You then just iterate over the values and you have the results you want.

You could even make your own implementation of a Map, which wraps a TreeMap and presents it as <K,Iterator<V>>. Something like:
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to JavaRanch
 
rahul pandia
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply guys...but as i have told you...I am new...so i need more explanation in this topic

and Sathiesh Kumar
I don't know about the nesting..
"You can use collections nesting each other to achieve anything like this.
Here you can use a List inside the Map, where you keep adding the items to the list on every increment."

Can you provide me with some material on it...so that i can understand it....

AND Thank you for welcoming me in the community......
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The material you need is a pencil and paper. Write down what you want, and a data structure which looks like that. It looks as if you need a "tail" as Steve Luke mentioned.
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic