I have a map Map<K, V> and my goal is to remove the duplicated values and output the very same structure Map<K, V> again. In case the duplicated value is found, there must be selected one key (k) from the two keys (k1 and k2) which hold these values, for this reason, assume the BinaryOperator<K> giving k from k1 and k2 is available.
Example input and output:
// 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
My attempt using Stream::collect(Supplier, BiConsumer, BiConsumer) is a bit very clumsy and contains mutable operations such as Map::put and Map::remove which I would like to avoid:
// // 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
);
Is there a solution using an appropriate combination of Collectors within one Stream::collect call (e.g. without mutable operations)?
You can use 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));
}
Try this: Simple way is inverse the key and value then use toMap() collector with merge function.
map.entrySet().stream()
.map(entry -> new AbstractMap.SimpleEntry<>(entry.getValue(), entry.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, reducingKeysBinaryOperator));
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));
I find the non-streams solution more expressive:
BinaryOperator<K> reducingKeysBinaryOperator = (k1, k2) -> k1 > k2 ? k1 : k2;
Map<V, K> reverse = new LinkedHashMap<>(map.size());
map.forEach((k, v) -> reverse.merge(v, k, reducingKeysBinaryOperator));
Map<K, V> result = new LinkedHashMap<>(reverse.size());
reverse.forEach((v, k) -> result.put(k, v));
This uses Map.merge with your reducing bi-function and uses LinkedHashMap to preserve original entries order.
I found a way of using only Collectors with no need for collecting and further processing the returned Map again. The idea is:
Group the Map<K, V> to Map<V, List<K>.
Map<K, V> distinctValuesMap = this.stream.collect(
Collectors.collectingAndThen(
Collectors.groupingBy(Entry::getValue),
groupingDownstream
)
);
{apple=[1, 5, 3], orange=[4, 2]}
Reduce the new keys (List<K>) to K using BinaryOperator<K>.
Function<Entry<V, List<Entry<K, V>>>, K> keyMapFunction = e -> e.getValue().stream()
.map(Entry::getKey)
.collect(Collectors.collectingAndThen(
Collectors.reducing(reducingKeysBinaryOperator),
Optional::get
)
);
{apple=5, orange=4}
Inverse the Map<V, K> back to Map<K, V> structure again - which is safe since both keys and values are guaranteed as distinct.
Function<Map<V, List<Entry<K,V>>>, Map<K, V>> groupingDownstream = m -> m.entrySet()
.stream()
.collect(Collectors.toMap(
keyMapFunction,
Entry::getKey
)
);
{5=apple, 4=orange}
The final code:
final BinaryOperator<K> reducingKeysBinaryOperator = ...
final Map<K, V> distinctValuesMap = map.entrySet().stream().collect(
Collectors.collectingAndThen(
Collectors.groupingBy(Entry::getValue),
m -> m.entrySet().stream().collect(
Collectors.toMap(
e -> e.getValue().stream().map(Entry::getKey).collect(
Collectors.collectingAndThen(
Collectors.reducing(reducingKeysBinaryOperator),
Optional::get
)
),
Entry::getKey
)
)
)
);
Another approch to get the desired result with "Stream and Collectors.groupingBy".
map = map.entrySet().stream()
.collect(Collectors.groupingBy(
Entry::getValue,
Collectors.maxBy(Comparator.comparing(Entry::getKey))
)
)
.entrySet().stream()
.collect(Collectors.toMap(
k -> {
return k.getValue().get().getKey();
},
Entry::getKey));
I want to convert inner map from map of maps.
Old map: Map<String, Map<LocalDate, Integer>> Integer means seconds
New map: Map<String, Map<LocalDate, Duration>>
I have tried created new inner map, but got an error
Error: java: no suitable method found for putAll(java.util.stream.Stream<java.lang.Object>)
method java.util.Map.putAll(java.util.Map<? extends java.time.LocalDate,? extends java.time.Duration>) is not applicable
oldMap.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey,
e -> new HashMap<LocalDate, Duration>() {{
putAll(
e.getValue().entrySet().stream()
.map(x -> new HashMap.SimpleEntry<LocalDate, Duration>
(x.getKey(), Duration.ofSeconds(x.getValue())))
);
}}
));
If you want compact code, you may use
Map<String, Map<LocalDate, Duration>> newMap = new HashMap<>();
oldMap.forEach((s,o) -> o.forEach((d, i) ->
newMap.computeIfAbsent(s, x->new HashMap<>()).put(d, Duration.ofSeconds(i))));
If you want to avoid unnecessary hash operations, you may expand it a bit
Map<String, Map<LocalDate, Duration>> newMap = new HashMap<>();
oldMap.forEach((s,o) -> {
Map<LocalDate, Duration> n = new HashMap<>();
newMap.put(s, n);
o.forEach((d, i) -> n.put(d, Duration.ofSeconds(i)));
});
Quick and clean
HashMap<String, HashMap<LocalDate, Duration>> newMap = new HashMap<>();
oldHashMap.forEach((key, innerMap) -> {
HashMap<LocalDate, Duration> newStuff = new HashMap<>();
innerMap.forEach((k2,v2) -> newStuff.put(k2,Duration.ofSeconds(v2)));
newMap.put(key, newStuff);
});
And one more...
Map<String, Map<LocalDate, Duration>> newMap = map.entrySet().stream().collect(
Collectors.toMap(Entry::getKey,
entry -> entry.getValue().entrySet().stream().collect(
Collectors.toMap(Entry::getKey, e -> Duration.ofSeconds(e.getValue())))));
My two cents, create a method to transform Map<K, V1> to a Map<K, V2>:
public static <K,V1,V2> Map<K, V2> transformValues(final Map<K, V1> input, final Function<V1,V2> transform) {
Function<Map.Entry<K, V1>, V2> mapper = transform.compose(Map.Entry::getValue);
return input.entrySet().stream()
.collect(toMap(Map.Entry::getKey, mapper));
}
Then your code becomes:
Map<String, Map<LocalDate, Duration>> transformed
= transformValues(maps, map -> transformValues(map, Duration::ofSeconds));
Let's extract a helper method toMap to makes the problem more simpler.
Map<String, Map<LocalDate, Duration>> result = oldMap.entrySet().stream()
.map(entry -> new AbstractMap.SimpleEntry<>(
entry.getKey(),
entry.getValue().entrySet().stream()
.collect(toMap(it -> Duration.ofSeconds(it.longValue())))
// ^--- mapping Integer to Duration
)).collect(toMap(Function.identity()));
<K, V, R> Collector<Map.Entry<K, V>, ?, Map<K, R>> toMap(Function<V, R> mapping) {
return Collectors.toMap(
Map.Entry::getKey,
mapping.compose(Map.Entry::getValue)
);
}
AND you can simplified the code as further since the primary logic is duplicated: adapts a value to another value in a Map.
Function<
Map<String, Map<LocalDate, Integer>>,
Map<String, Map<LocalDate, Duration>>
> mapping = mapping(mapping(seconds -> Duration.ofSeconds(seconds.longValue())));
// | |
// | adapts Map<LocalDate, Integer> to Map<LocalDate,Duration>
// |
//adapts Map<String,Map<LocalDate,Integer>> to Map<String,Map<LocalDate,Duration>>
Map<String, Map<LocalDate, Duration>> result = mapping.apply(oldMap);
<K, V1, V2> Function<Map<K, V1>, Map<K, V2>> mapping(Function<V1, V2> mapping) {
return it -> it.entrySet().stream().collect(toMap(mapping));
}
THEN you can use it anywhere that needs a Function<Map<?,?>,Map<?,?>> to adapts the value to another value,for example:
Map<String, Map<LocalDate, Duration>> result = Optional.of(oldMap)
.map(mapping(mapping(seconds -> Duration.ofSeconds(seconds.longValue()))))
.get();
If i understand you correct,you can see following code:
oldMap.entrySet().stream()
.collect(Collectors.toMap(x -> x.getKey(),
x -> {
Map<LocalDate, Duration> temp = new HashMap<>();
x.getValue().forEach((k, v) -> {
temp.put(k, Duration.ofSeconds(v));
});
return temp;
})
);
For completeness, here's a version that uses Guava's Maps.transformValues:
Map<String, Map<LocalDate, Duration>> result =
Maps.transformValues(oldMap, m -> Maps.transformValues(m, Duration::ofSeconds));
I would prefer to iterate over the entries of your old map and streaming over the inner map:
for (Entry<String, Map<LocalDate, Integer>> entry : oldMap.entrySet()) {
Map<LocalDate, Duration> asDuration = entry.getValue().entrySet().stream()
.collect(Collectors.toMap(e -> e.getKey(), e -> Duration.ofSeconds(e.getValue().longValue())));
newMap.put(entry.getKey(), asDuration);
}
Otherwise you need a second stream inside your collect:
newMap = oldMap.entrySet().stream()
.collect(Collectors.toMap(s -> s.getKey(), s -> s.getValue().entrySet().stream()
.collect(Collectors.toMap(e -> e.getKey(), e -> Duration.ofSeconds(e.getValue().longValue())))));
Here is solution by StreamEx:
EntryStream.of(oldMap)
.mapValues(v -> EntryStream.of(v).mapValues(Duration::ofSeconds).toMap())
.toMap();
I have two HashMap objects defined like so:
HashMap<String, Integer> map1 = new HashMap<String, Integer>();
HashMap<String, Integer> map2 = new HashMap<String, Integer>();
I also have a third HashMap object:
HashMap<String, Integer> map3;
How can I merge map1 and map2 together into map3?
map3 = new HashMap<>();
map3.putAll(map1);
map3.putAll(map2);
If you know you don't have duplicate keys, or you want values in map2 to overwrite values from map1 for duplicate keys, you can just write
map3 = new HashMap<>(map1);
map3.putAll(map2);
If you need more control over how values are combined, you can use Map.merge, added in Java 8, which uses a user-provided BiFunction to merge values for duplicate keys. merge operates on individual keys and values, so you'll need to use a loop or Map.forEach. Here we concatenate strings for duplicate keys:
map3 = new HashMap<>(map1);
for (Map.Entry<String, String> e : map2.entrySet())
map3.merge(e.getKey(), e.getValue(), String::concat);
//or instead of the above loop
map2.forEach((k, v) -> map3.merge(k, v, String::concat));
If you know you don't have duplicate keys and want to enforce it, you can use a merge function that throws an AssertionError:
map2.forEach((k, v) ->
map3.merge(k, v, (v1, v2) ->
{throw new AssertionError("duplicate values for key: "+k);}));
Taking a step back from this specific question, the Java 8 streams library provides toMap and groupingBy Collectors. If you're repeatedly merging maps in a loop, you may be able to restructure your computation to use streams, which can both clarify your code and enable easy parallelism using a parallel stream and concurrent collector.
One-liner using Java 8 Stream API:
map3 = Stream.of(map1, map2).flatMap(m -> m.entrySet().stream())
.collect(Collectors.toMap(Entry::getKey, Entry::getValue))
Among the benefits of this method is ability to pass a merge function, which will deal with values that have the same key, for example:
map3 = Stream.of(map1, map2).flatMap(m -> m.entrySet().stream())
.collect(Collectors.toMap(Entry::getKey, Entry::getValue, Math::max))
Java 8 alternative one-liner for merging two maps:
defaultMap.forEach((k, v) -> destMap.putIfAbsent(k, v));
The same with method reference:
defaultMap.forEach(destMap::putIfAbsent);
Or idemponent for original maps solution with third map:
Map<String, Integer> map3 = new HashMap<String, Integer>(map2);
map1.forEach(map3::putIfAbsent);
And here is a way to merge two maps into fast immutable one with Guava that does least possible intermediate copy operations:
ImmutableMap.Builder<String, Integer> builder = ImmutableMap.<String, Integer>builder();
builder.putAll(map1);
map2.forEach((k, v) -> {if (!map1.containsKey(k)) builder.put(k, v);});
ImmutableMap<String, Integer> map3 = builder.build();
See also Merge two maps with Java 8 for cases when values present in both maps need to be combined with mapping function.
If you don't need mutability for your final map, there is Guava's ImmutableMap with its Builder and putAll method which, in contrast to Java's Map interface method, can be chained.
Example of use:
Map<String, Integer> mergeMyTwoMaps(Map<String, Integer> map1, Map<String, Integer> map2) {
return ImmutableMap.<String, Integer>builder()
.putAll(map1)
.putAll(map2)
.build();
}
Of course, this method can be more generic, use varargs and loop to putAll Maps from arguments etc. but I wanted to show a concept.
Also, ImmutableMap and its Builder have few limitations (or maybe features?):
they are null hostile (throw NullPointerException - if any key or value in map is null)
Builder don't accept duplicate keys (throws IllegalArgumentException if duplicate keys were added).
HashMap has a putAll method.
http://download.oracle.com/javase/6/docs/api/java/util/HashMap.html
You could use Collection.addAll() for other types, e.g. List, Set, etc. For Map, you can use putAll.
Generic solution for combining two maps which can possibly share common keys:
In-place:
public static <K, V> void mergeInPlace(Map<K, V> map1, Map<K, V> map2,
BinaryOperator<V> combiner) {
map2.forEach((k, v) -> map1.merge(k, v, combiner::apply));
}
Returning a new map:
public static <K, V> Map<K, V> merge(Map<K, V> map1, Map<K, V> map2,
BinaryOperator<V> combiner) {
Map<K, V> map3 = new HashMap<>(map1);
map2.forEach((k, v) -> map3.merge(k, v, combiner::apply));
return map3;
}
Very late but let me share what I did when I had the same issue.
Map<String, List<String>> map1 = new HashMap<>();
map1.put("India", Arrays.asList("Virat", "Mahi", "Rohit"));
map1.put("NZ", Arrays.asList("P1","P2","P3"));
Map<String, List<String>> map2 = new HashMap<>();
map2.put("India", Arrays.asList("Virat", "Mahi", "Rohit"));
map2.put("NZ", Arrays.asList("P1","P2","P4"));
Map<String, List<String>> collect4 = Stream.of(map1, map2)
.flatMap(map -> map.entrySet().stream())
.collect(
Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(strings, strings2) -> {
List<String> newList = new ArrayList<>();
newList.addAll(strings);
newList.addAll(strings2);
return newList;
}
)
);
collect4.forEach((s, strings) -> System.out.println(s+"->"+strings));
It gives the following output
NZ->[P1, P2, P3, P1, P2, P4]
India->[Virat, Mahi, Rohit, Virat, Mahi, Rohit]
A small snippet I use very often to create map from other maps:
static public <K, V> Map<K, V> merge(Map<K, V>... args) {
final Map<K, V> buffer = new HashMap<>();
for (Map m : args) {
buffer.putAll(m);
}
return buffer;
}
you can use HashMap<String, List<Integer>> to merge both hashmaps and avoid losing elements paired with the same key.
HashMap<String, Integer> map1 = new HashMap<>();
HashMap<String, Integer> map2 = new HashMap<>();
map1.put("key1", 1);
map1.put("key2", 2);
map1.put("key3", 3);
map2.put("key1", 4);
map2.put("key2", 5);
map2.put("key3", 6);
HashMap<String, List<Integer>> map3 = new HashMap<>();
map1.forEach((str, num) -> map3.put(str, new ArrayList<>(Arrays.asList(num))));
//checking for each key if its already in the map, and if so, you just add the integer to the list paired with this key
for (Map.Entry<String, Integer> entry : map2.entrySet()) {
Integer value = entry.getValue();
String key = entry.getKey();
if (map3.containsKey(key)) {
map3.get(key).add(value);
} else {
map3.put(key, new ArrayList<>(Arrays.asList(value)));
}
}
map3.forEach((str, list) -> System.out.println("{" + str + ": " + list + "}"));
output:
{key1: [1, 4]}
{key2: [2, 5]}
{key3: [3, 6]}
You can use putAll function for Map as explained in the code below
HashMap<String, Integer> map1 = new HashMap<String, Integer>();
map1.put("a", 1);
map1.put("b", 2);
map1.put("c", 3);
HashMap<String, Integer> map2 = new HashMap<String, Integer>();
map1.put("aa", 11);
map1.put("bb", 12);
HashMap<String, Integer> map3 = new HashMap<String, Integer>();
map3.putAll(map1);
map3.putAll(map2);
map3.keySet().stream().forEach(System.out::println);
map3.values().stream().forEach(System.out::println);
Below snippet takes more than one map and combine them.
private static <K, V> Map<K, V> combineMaps(Map<K, V>... maps) {
if (maps == null || maps.length == 0) {
return Collections.EMPTY_MAP;
}
Map<K, V> result = new HashMap<>();
for (Map<K, V> map : maps) {
result.putAll(map);
}
return result;
}
Demo example link.
Assuming the following input:
import java.util.stream.Stream;
import java.util.stream.Collectors;
import java.util.Map;
...
var m1 = Map.of("k1", 1, "k2", 2);
var m2 = Map.of("k3", 3, "k4", 4);
When you're sure not to have any key collisions between both input maps, a simple expression that avoids any mutations and yields an immutable result could be:
var merged = Stream.concat(
m1.entrySet().stream(),
m2.entrySet().stream()
).collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue));
In case key collisions are possible, we can provide a lambda to specify how to de-duplicate them. For example if we'd like to keep the largest value in case an entry is present in both input, we could:
.collect(Collectors.toUnmodifiableMap(
Map.Entry::getKey,
Map.Entry::getValue,
Math::max)) // any function (Integer, Integer) -> Integer is ok here
HashMap<Integer,String> hs1 = new HashMap<>();
hs1.put(1,"ram");
hs1.put(2,"sita");
hs1.put(3,"laxman");
hs1.put(4,"hanuman");
hs1.put(5,"geeta");
HashMap<Integer,String> hs2 = new HashMap<>();
hs2.put(5,"rat");
hs2.put(6,"lion");
hs2.put(7,"tiger");
hs2.put(8,"fish");
hs2.put(9,"hen");
HashMap<Integer,String> hs3 = new HashMap<>();//Map is which we add
hs3.putAll(hs1);
hs3.putAll(hs2);
System.out.println(" hs1 : " + hs1);
System.out.println(" hs2 : " + hs2);
System.out.println(" hs3 : " + hs3);
Duplicate items will not be added(that is duplicate keys) as when we will print hs3 we will get only one value for key 5 which will be last value added and it will be rat.
**[Set has a property of not allowing the duplicate key but values can be duplicate]
Method 1: Put maps in a List and then join
public class Test15 {
public static void main(String[] args) {
Map<String, List<String>> map1 = new HashMap<>();
map1.put("London", Arrays.asList("A", "B", "C"));
map1.put("Wales", Arrays.asList("P1", "P2", "P3"));
Map<String, List<String>> map2 = new HashMap<>();
map2.put("Calcutta", Arrays.asList("Protijayi", "Gina", "Gini"));
map2.put("London", Arrays.asList( "P4", "P5", "P6"));
map2.put("Wales", Arrays.asList( "P111", "P5555", "P677666"));
System.out.println(map1);System.out.println(map2);
// put the maps in an ArrayList
List<Map<String, List<String>>> maplist = new ArrayList<Map<String,List<String>>>();
maplist.add(map1);
maplist.add(map2);
/*
<T,K,U> Collector<T,?,Map<K,U>> toMap(
Function<? super T,? extends K> keyMapper,
Function<? super T,? extends U> valueMapper,
BinaryOperator<U> mergeFunction)
*/
Map<String, List<String>> collect = maplist.stream()
.flatMap(ch -> ch.entrySet().stream())
.collect(
Collectors.toMap(
//keyMapper,
Entry::getKey,
//valueMapper
Entry::getValue,
// mergeFunction
(list_a,list_b) -> Stream.concat(list_a.stream(), list_b.stream()).collect(Collectors.toList())
));
System.out.println("Final Result(Map after join) => " + collect);
/*
{Wales=[P1, P2, P3], London=[A, B, C]}
{Calcutta=[Protijayi, Gina, Gini], Wales=[P111, P5555, P677666], London=[P4, P5, P6]}
Final Result(Map after join) => {Calcutta=[Protijayi, Gina, Gini], Wales=[P1, P2, P3, P111, P5555, P677666], London=[A, B, C, P4, P5, P6]}
*/
}//main
}
Method 2 : Normal Map merge
public class Test15 {
public static void main(String[] args) {
Map<String, List<String>> map1 = new HashMap<>();
map1.put("London", Arrays.asList("A", "B", "C"));
map1.put("Wales", Arrays.asList("P1", "P2", "P3"));
Map<String, List<String>> map2 = new HashMap<>();
map2.put("Calcutta", Arrays.asList("Protijayi", "Gina", "Gini"));
map2.put("London", Arrays.asList( "P4", "P5", "P6"));
map2.put("Wales", Arrays.asList( "P111", "P5555", "P677666"));
System.out.println(map1);System.out.println(map2);
/*
<T,K,U> Collector<T,?,Map<K,U>> toMap(
Function<? super T,? extends K> keyMapper,
Function<? super T,? extends U> valueMapper,
BinaryOperator<U> mergeFunction)
*/
Map<String, List<String>> collect = Stream.of(map1,map2)
.flatMap(ch -> ch.entrySet().stream())
.collect(
Collectors.toMap(
//keyMapper,
Entry::getKey,
//valueMapper
Entry::getValue,
// mergeFunction
(list_a,list_b) -> Stream.concat(list_a.stream(), list_b.stream()).collect(Collectors.toList())
));
System.out.println("Final Result(Map after join) => " + collect);
/*
{Wales=[P1, P2, P3], London=[A, B, C]}
{Calcutta=[Protijayi, Gina, Gini], Wales=[P111, P5555, P677666], London=[P4, P5, P6]}
Final Result(Map after join) => {Calcutta=[Protijayi, Gina, Gini], Wales=[P1, P2, P3, P111, P5555, P677666], London=[A, B, C, P4, P5, P6]}
*/
}//main
}
In Python, HashMap is called Dictionary and we can merge them very easily.
x = {'Roopa': 1, 'Tabu': 2}
y = {'Roopi': 3, 'Soudipta': 4}
z = {**x,**y}
print(z)
{'Roopa': 1, 'Tabu': 2, 'Roopi': 3, 'Soudipta': 4}
you can use - addAll method
http://download.oracle.com/javase/6/docs/api/java/util/HashMap.html
But there is always this issue that - if your two hash maps have any key same - then it will override the value of the key from first hash map with the value of the key from second hash map.
For being on safer side - change the key values - you can use prefix or suffix on the keys - ( different prefix/suffix for first hash map and different prefix/suffix for second hash map )