• Post Reply Bookmark Topic Watch Topic
  • New Topic

In Collections  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In My Scenario.
I've stored the following,
[1,20]
[2,10]
[3,14]
[4,20]
[5,14]
[6,20]

Key and values pair into the HashMap.but i've to display keys such as 1,4,6,because these values all are same.
Is it possible using HashMap?
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is not what Maps do; if you enter 1, 20 . . . 4, 20, you will get two independent key-value pairs.
Try putting the 20 as a Key, and a List<SomeNumberOrOther> as the Value.
If you already have 20, you can use the get method->not null, or the containsKey method->true, then get the List and add the number to it.
If you don't already have 20, get will return null and containsKey will return false, then you can put a new List as a Value, and add the number to it.

I hope you can understand that; if not ask again
 
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With Campbell's idea you would end up with a stucture similar to this based off your example:

[20, [1,4,6]]
[10, [2]]
[14, [3,5]]

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!