就像ImmutableList
如何扩展一样:
ImmutableList<Long> originalList = ImmutableList.of(1, 2, 3);
ImmutableList<Long> extendedList = Iterables.concat(originalList, ImmutableList.of(4, 5));
如果我有现有地图,我该如何扩展它(或创建一个包含替换值的新副本)?
ImmutableMap<String, Long> oldPrices = ImmutableMap.of("banana", 4, "apple", 7);
ImmutableMap<String, Long> newPrices = … // Increase apple prices, leave others.
// => { "banana": 4, "apple": 9 }
(我们不寻求有效的解决方案,显然that doesn't exist by design。这个问题反而寻求最惯用的解决方案。)
答案 0 :(得分:18)
您可以明确创建构建器:
ImmutableMap<String, Long> oldPrices = ImmutableMap.of("banana", 4, "apple", 7);
ImmutableMap<String, Long> newPrices =
new ImmutableMap.Builder()
.putAll(oldPrices)
.put("orange", 9)
.build();
编辑:
如评论中所述,这不允许覆盖现有值。这可以通过遍历不同Map
的初始化块(例如,HashMap
)来完成。它不是优雅的,但应该有效:
ImmutableMap<String, Long> oldPrices = ImmutableMap.of("banana", 4, "apple", 7);
ImmutableMap<String, Long> newPrices =
new ImmutableMap.Builder()
.putAll(new HashMap<>() {{
putAll(oldPrices);
put("orange", 9); // new value
put("apple", 12); // override an old value
}})
.build();
答案 1 :(得分:6)
只需将ImmutableMap
复制到新的HashMap
,添加项目,然后转换为新的ImmutableMap
ImmutableMap<String, Long> oldPrices = ImmutableMap.of("banana", 4, "apple", 7);
Map<String, Long> copy = new HashMap<>(oldPrices);
copy.put("orange", 9); // add a new entry
copy.put("apple", 12); // replace the value of an existing entry
ImmutableMap<String, Long> newPrices = ImmutableMap.copyOf(copy);
答案 2 :(得分:1)
与Guava保持联系,您可以创建一个实用程序方法,在构建新地图时跳过重复项。这是在Maps.filterKeys()
的帮助下完成的。
这是效用函数:
private <T, U> ImmutableMap<T, U> extendMap(ImmutableMap<T, U> original, ImmutableMap<T, U> changes) {
return ImmutableMap.<T, U>builder()
.putAll(changes)
.putAll(Maps.filterKeys(original, key -> !changes.containsKey(key)))
.build();
}
这是一个包含数据的单元测试(基于AssertJ)。
@Test
public void extendMap() {
ImmutableMap<String, Integer> oldPrices = ImmutableMap.of("banana", 4, "apple", 7);
ImmutableMap<String, Integer> changes = ImmutableMap.of("orange", 9, "apple", 12);
ImmutableMap<String, Integer> newPrices = extendMap(oldPrices, changes);
assertThat(newPrices).contains(
entry("banana", 4),
entry("apple", 12),
entry("orange", 9));
}
更新:对于基于Maps.difference()
的效用函数,这是一个稍微优雅的替代方案。
private <T, U> ImmutableMap<T, U> extendMap(ImmutableMap<T, U> original, ImmutableMap<T, U> changes) {
return ImmutableMap.<T, U>builder()
.putAll(Maps.difference(original, changes).entriesOnlyOnLeft())
.putAll(changes)
.build();
}
答案 3 :(得分:0)
嗯,我用溪流完成了这项工作,但并不完美:
public static <K,V> Map<K,V> update(final Map<K,V> map, final Map.Entry<K,V> replace)
{
return Stream.concat(
Stream.of(replace),
map.entrySet().stream()
.filter(kv -> ! replace.getKey().equals(kv.getKey()))
.collect(Collectors.toMap(SimpleImmutableEntry::getKey, SimpleImmutableEntry::getValue));
}
这只会插入或更新单个条目。注意ImmutableMap&amp;相关的收集器可以放入(这是我实际使用的)
答案 4 :(得分:0)
不是一个非常高效的代码,但下面的代码可以正常工作
private <K, V> ImmutableMap.Builder<K, V> update(final ImmutableMap.Builder<K, V> builder, final List<ImmutablePair<K, V>> replace) {
Set<K> keys = replace.stream().map(entry -> entry.getKey()).collect(toSet());
Map<K, V> map = new HashMap<>();
builder.build().forEach((key, val) -> {
if (!keys.contains(key)) {
map.put(key, val);
}
});
ImmutableMap.Builder<K, V> newBuilder = ImmutableMap.builder();
newBuilder.putAll(map);
replace.stream().forEach(kvEntry -> newBuilder.put(kvEntry.getKey(), kvEntry.getValue()));
return newBuilder;
}