| Author |
Merging a set and hashmap
|
Arun Sanker
Ranch Hand
Joined: Mar 21, 2007
Posts: 44
|
|
Hi Guys, I have a set in which I store some unique userdefined objects,say a "Color" object. So my collection set will look as follows: set<Color> objects. For example:- (color1) (color2) (color3) (color4) (color5) (color6) (color7) (color8) (color9) (color10) I also have a HashMap which stores objects as follows: HashMap<Color,Color> objects(Also userdefined "Color" objects). For example:- (color1,color20) (color2,color21) (color3,color22) (color4,color23) (color4,color24) (color5,color25) My need is that I want to merge this set and hashmap so that the resulting collection looks as follows: (color1,color20) (color2,color21) (color3,color22) (color4,color23) (color4,color24) (color5,color25) (color6,null) (color7,null) (color8,null) (color9,null) (color10,null) How could this be achieved. Any help is appreciated. Thanks.... P
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32611
|
|
Whenever I see that sort of question, I start thinking questions myself, like, "why does anybody want to do that?" If you go through the Map interface you find methods like containsXXX. So you can find whether the Map contains that object as a key, and if not, put(colour99, null).
|
 |
Patricia Samuel
Ranch Hand
Joined: Sep 12, 2007
Posts: 300
|
|
Yes, You can do like this. if(!map.contains(key)){ map.add(value from set,null); } [ November 26, 2008: Message edited by: Patricia Samuel ]
|
 |
Arun Sanker
Ranch Hand
Joined: Mar 21, 2007
Posts: 44
|
|
|
Thanks a Lot....
|
 |
 |
|
|
subject: Merging a set and hashmap
|
|
|