2011-08-18 74 views
0

您好我有兩個哈希映射如下合併兩個多值包含HashMap基於價值

[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] 

如何才達到以上?

+0

我剛纔看到你有一個奇怪的鍵。你不應該使用Map作爲另一張地圖的關鍵字,因爲關鍵字不應該是可變的。 –

回答

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()); 
    } 
}