• Post Reply Bookmark Topic Watch Topic
  • New Topic

hashMap question  RSS feed

 
rahul pandia
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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!!







 
Sathiesh Kumar Vs
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • 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.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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:
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to JavaRanch
 
rahul pandia
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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: 56599
172
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!