Nik*_*las 12 java dictionary java-8 java-stream collectors
我有一张地图Map<K, V>,我的目标是删除重复的值并Map<K, V>再次输出相同的结构。如果重复的值被发现,必须有一个选择键(k从两个键()k1和k1)持有这些值,因为这个原因,假设BinaryOperator<K>给k从k1和k2可用。
示例输入和输出:
// Input
Map<Integer, String> map = new HashMap<>();
map.put(1, "apple");
map.put(5, "apple");
map.put(4, "orange");
map.put(3, "apple");
map.put(2, "orange");
// Output: {5=apple, 4=orange} // the key is the largest possible
Run Code Online (Sandbox Code Playgroud)
用我的尝试Stream::collect(Supplier, BiConsumer, BiConsumer)是位非常笨拙,包含可变操作,比如Map::put和Map::remove我想避免:
// // the key is the largest integer possible (following the example above)
final BinaryOperator<K> reducingKeysBinaryOperator = (k1, k2) -> k1 > k2 ? k1 : k2;
Map<K, V> distinctValuesMap = map.entrySet().stream().collect(
HashMap::new, // A new map to return (supplier)
(map, entry) -> { // Accumulator
final K key = entry.getKey();
final V value = entry.getValue();
final Entry<K, V> editedEntry = Optional.of(map) // New edited Value
.filter(HashMap::isEmpty)
.map(m -> new SimpleEntry<>(key, value)) // If a first entry, use it
.orElseGet(() -> map.entrySet() // otherwise check for a duplicate
.stream()
.filter(e -> value.equals(e.getValue()))
.findFirst()
.map(e -> new SimpleEntry<>( // .. if found, replace
reducingKeysBinaryOperator.apply(e.getKey(), key),
map.remove(e.getKey())))
.orElse(new SimpleEntry<>(key, value))); // .. or else leave
map.put(editedEntry.getKey(), editedEntry.getValue()); // put it to the map
},
(m1, m2) -> {} // Combiner
);
Run Code Online (Sandbox Code Playgroud)
是否有Collectors在一次Stream::collect调用中使用适当组合的解决方案(例如,没有可变操作)?
Mik*_*Hay 11
您可以使用Collectors.toMap
private Map<Integer, String> deduplicateValues(Map<Integer, String> map) {
Map<String, Integer> inverse = map.entrySet().stream().collect(toMap(
Map.Entry::getValue,
Map.Entry::getKey,
Math::max) // take the highest key on duplicate values
);
return inverse.entrySet().stream().collect(toMap(Map.Entry::getValue, Map.Entry::getKey));
}
Run Code Online (Sandbox Code Playgroud)
试试这个:简单的方法是反转键和值,然后使用toMap()带有合并功能的收集器。
map.entrySet().stream()
.map(entry -> new AbstractMap.SimpleEntry<>(entry.getValue(), entry.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, reducingKeysBinaryOperator));
Run Code Online (Sandbox Code Playgroud)
Map<K, V> output = map.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey, reducingKeysBinaryOperator))
.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
Run Code Online (Sandbox Code Playgroud)