只有在密鑰不存在的情況下,我需要將條目添加到地圖。在Java 8我只用putIfAbsent
,但我使用Groovy與Java 7如果在沒有Java的Groovy中缺席,請放到地圖中8
代碼能說明問題:
def map = [a: 1, b: 2, c: 3]
def newEntries = [a: 11, b: 22, d: 44]
def result = // put new entries to the map only if they are not present
assert result == [a: 1, b: 2, c: 3, d: 44]
是否有可能使用一些Groovy的功能來做到這一點還是我需要手動執行?
這難道不是一種重複:http://stackoverflow.com/questions/15640525/how-do-i-add-multiple-groovy-map-entries-without-overwriting-the-current-entries? – Opal
不,我不需要'Multimap',只是'HashMap'對我的問題很好 –