Java tutorial
//package com.java2s; //License from project: Open Source License import java.util.Collection; import java.util.Map; import java.util.function.BinaryOperator; import java.util.stream.Collectors; import java.util.stream.Stream; public class Main { @SafeVarargs public static <K, V> Map<K, V> preservedUnionMap(Map<K, V>... maps) { return unionMap(true, maps); } @SafeVarargs public static <K, V> Map<K, V> unionMap(Map<K, V>... maps) { return unionMap(false, maps); } @SafeVarargs public static <K, V> Map<K, V> unionMap(boolean preserved, Map<K, V>... maps) { return Stream.of(maps).map(Map::entrySet).flatMap(Collection::stream).collect(Collectors .toMap(Map.Entry::getKey, Map.Entry::getValue, preserved ? preserveMerger() : abandonMerger())); } public static <T> BinaryOperator<T> preserveMerger() { return mergeDuplicates(false); } public static <T> BinaryOperator<T> abandonMerger() { return mergeDuplicates(true); } public static <T> BinaryOperator<T> mergeDuplicates(boolean override) { return (u, v) -> override ? v : u; } }