基于值合并两个多值哈希映射

时间:2011-08-18 15:19:55

标签: java

您好我有两个哈希映射如下

[AdGenres-b:key:[177], SongTitle:[What I've Done], ArtistName:[Linkin Park], MusicVideoRating:[TV-14], PlayId:[1367], AdGenres-b:value:[Rock], MusicVideoProvider:[Warner Music Group], AssetId:[91744]]

[AdGenres-b:key:[184], SongTitle:[What I've Done], ArtistName:[Linkin Park], MusicVideoRating:[TV-14], PlayId:[1367], AdGenres-b:value:[Rock - Alternative], MusicVideoProvider:[Warner Music Group], AssetId:[91744]]

我想要结果图:

[AdGenres-b:key:[177, 184],SongTitle:[What I've Done], ArtistName:[Linkin Park], MusicVideoRating:[TV-14], PlayId:[1367], AdGenres-b:value:[Rock - Rock, Rock - Alternative], MusicVideoProvider:[Warner Music Group], AssetId:[91744]

如何实现上述目标?

1 个答案:

答案 0 :(得分:0)

简单方法:使用Guava Multimap

Multimap<String, String> mmap = HashMultimap.create();
for(Map<String, String> map : Arrays.asList(map1,map2)){
    for(Entry<String, String> entry:map.entrySet()){
        mmap.put(entry.getKey(), entry.getValue());
    }
}