• Post Reply Bookmark Topic Watch Topic
  • New Topic

Merging a set and hashmap  RSS feed

 
Arun Sanker
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a Lot....
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!