permaculture playing cards*
The moose likes Beginning Java and the fly likes List in HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "List in HashMap" Watch "List in HashMap" New topic
Author

List in HashMap

AbhiRam Kalyan
Greenhorn

Joined: Oct 31, 2005
Posts: 25
Hello all,
I want to iterate through the list in HashMap and store the values as key and keys as values in a new hashmap.

Here is the HashMap with array list:
{secondval=[A3], firstval=[A2, A1], thirdval=[A4]}
Now i would like to store this in a new hashmap as {A1=firstval,A2=firstval,A3=thirdval,A4=fourthval}..

Please tell me what is the efficient way to iterate and store values in a new Hashmap.

Sasikanth
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

Check out TreeMap and its constructors.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
AbhiRam Kalyan
Greenhorn

Joined: Oct 31, 2005
Posts: 25
didnt get.could you show me an example?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38793
    
  23
What didn't you understand about Maps or their implementations, particularly HashMap and TreeMap and how they sort or order their entries?
AbhiRam Kalyan
Greenhorn

Joined: Oct 31, 2005
Posts: 25
I understand about sort order and treemap. What i am trying to do is to create a new hashmap from
{secondval=[A3], firstval=[A2, A1], thirdval=[A4]}
which should represent as
Key Value
A1 firstval
A2 firstval
A3 secondval
A4 thirdval
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38793
    
  23
So each entry consists of a "V" for value and an array of "K" for keys which it should be applied to. And you want it to order with the "K"s sorted. I would repeat what Rob said: look at TreeMap and read its constructors; there are four overloaded constructors to read about. I presume you have some sort of Comparator already, or A1 and A2 implement Comparable.
AbhiRam Kalyan
Greenhorn

Joined: Oct 31, 2005
Posts: 25
Thanks for all your support.. i got the solution.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38793
    
  23
Well done Please tell us what the solution was, so anybody having the same problem in the future can search for it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: List in HashMap