白衣染霜花
您可能想这样做:// Initializing the mapMap<String, List<Integer>> map = new LinkedHashMap<String, List<Integer>>() { { put("A", new ArrayList<>(Arrays.asList(1, 2, 3, 4))); put("B", new ArrayList<>(Arrays.asList(5, 6, 1, 7))); put("C", new ArrayList<>(Arrays.asList(8, 1, 5, 9))); }};// finding the common elementsList<Integer> allElements = map.values().stream().flatMap(List::stream).collect(Collectors.toList());Set<Integer> allDistinctElements = new HashSet<>();Set<Integer> commonElements = new HashSet<>();allElements.forEach(element -> { if(!allDistinctElements.add(element)) { commonElements.add(element); }});// removing the common elementsmap.forEach((key, list) -> list.removeAll(commonElements));// printing the mapmap.forEach((key, list) -> System.out.println(key + " = " + list));输出:A = [2, 3, 4]B = [6, 7]C = [8, 9]