You can use Collectors.toMap(), to make a new Map with zone id as the key and Zone as the value, if there is a duplicate then take the List<Part> from the second Zone and append it to the first one:

  Map<Integer, Zone> map = z.getZones().stream()
                            .collect(Collectors.toMap(Zone::getId, Function.identity(), 
                             (zone1, zone2) -> {
                               zone1.getParts().addAll(zone2.getParts());
                               return zone1;
                             }));
Answer from Fullstack Guy on Stack Overflow
🌐
Javaprogramto
javaprogramto.com › 2020 › 07 › java-8-convert-list-to-map-handling-duplicate-keys.html
Java 8 - Convert List to Map (Handling Duplicate Keys) JavaProgramTo.com
Employees list values are : ... Run the full code as below. toMap() method, we are passing getId() as the key that means id should be unique in the list but it has duplicates for id 102....
🌐
Java67
java67.com › 2018 › 01 › how-to-convert-list-into-map-in-java-8.html
How to Convert a List<V> to Map<K,V> in Java 8 - Example Tutorial | Java67
Map<String, Course > result = ... e2, LinkedHashMap::new)); Here in case of a duplicate, we are using the second element as a key to generated LinkedHashMap....
🌐
Baeldung
baeldung.com › home › java › java collections › java map › how to store duplicate keys in a map in java?
How to Store Duplicate Keys in a Map in Java? | Baeldung
January 8, 2024 - Map<String, List<String>> map = new HashMap<>(); List<String> list = new ArrayList<>(); map.put("key1", list); map.get("key1").add("value1"); map.get("key1").add("value2"); assertThat(map.get("key1").get(0)).isEqualTo("value1"); assertThat(map.get("key1").get(1)).isEqualTo("value2"); However, this verbose solution has multiple drawbacks and is prone to errors. It implies that we need to instantiate a Collection for every value, check for its presence before adding or removing a value, delete it manually when no values are left, etc. From Java 8, we could exploit the compute() methods and improve it:
🌐
Javabycode
javabycode.com › home › java 8 › java 8 list to map duplicate keys
Java 8 List to Map duplicate keys - Learn Java by Examples
March 6, 2020 - Today, I introduce you to Java 8 List to Map duplicate keys examples. Here, we will use Collectors.toMap() method to convert a List of an object into a Map. One more thing, If List has duplicates but Map doesn’t allow duplicate keys.
🌐
Javaprogramto
javaprogramto.com › 2020 › 07 › java-8-convert-list-to-map-handling-duplicate-keys.html
Java 8 - Convert List to Map (Handling Duplicate Keys) | JavaProgramTo.com
Employees list values are : ... Run the full code as below. toMap() method, we are passing getId() as the key that means id should be unique in the list but it has duplicates for id 102....
🌐
Mkyong
mkyong.com › home › java8 › java 8 – convert list to map
Java 8 - Convert List to Map - Mkyong.com
May 26, 2017 - 2.2 To solve the duplicated key issue above, pass in the third mergeFunction argument like this : Map<String, Long> result1 = list.stream().collect( Collectors.toMap(Hosting::getName, Hosting::getWebsites, (oldValue, newValue) -> oldValue ) ); ... ...
Find elsewhere
🌐
How to do in Java
howtodoinjava.com › home › collections framework › convert list to map in java
Convert List to Map in Java
September 23, 2022 - Notice the program output, all the values associated with duplicate employee-id ‘1’ and ‘2’ are maintained in a List and it returns a Map with key as Integer and value as List<Employee>.
🌐
Baeldung
baeldung.com › home › java › java collections › java map › handle duplicate keys when producing map using java stream
Handle Duplicate Keys When Producing Map Using Java Stream | Baeldung
January 8, 2024 - Therefore, using the multi-value map approach can solve the key duplication problem. However, this approach returns Map<String, List<City>>. If we require Map<String, City> as the return type, we can no longer group the objects with the duplicated key together in a collection.
🌐
Javathinking
javathinking.com › blog › convert-list-to-map-java-8
Convert List to Map in Java 8 | JavaThinking.com
June 30, 2025 - It takes three main parameters: a key mapper, a value mapper, and a merge function. The key mapper is a function that extracts the key from each element in the stream, the value mapper extracts the value, and the merge function is used to handle cases where there are duplicate keys.
🌐
CopyProgramming
copyprogramming.com › howto › how-to-add-duplicate-values-to-a-map-duplicate
Java: Adding Duplicate Values to a Map – How to Do It
July 23, 2023 - Java 8 -> Collectors.toMap -> Duplicate key, Indeed, YUM refers to one of the values (Currency) processed by toMap() that have a duplicate key, not the key value itself. It is a Java bug fixed in Java 9. To solve your issue, either use Collectors.groupingBy() to collect to a Map<Integer, List<Currency>> and in this case you could have multiple values by …
🌐
HowToDoInJava
howtodoinjava.com › home › java 8 › java collectors.tomap(): collecting stream items into map
Java Collectors.toMap(): Collecting Stream Items into Map
October 14, 2023 - For the demo purpose, we are using ... for a key in a List, we can use Collectors.groupingBy() to collect elements in Map<key, List<value>> format....
🌐
Blogger
javarevisited.blogspot.com › 2016 › 04 › 10-examples-of-converting-list-to-map.html
10 Examples of Converting a List to Map in Java 8
When you are converting List to Map, you must pay attention to a different characteristic of these two collection classes, a List allows duplicate elements, but Map doesn't allow duplicate keys...
🌐
Javadevcentral
javadevcentral.com › collectors tomap duplicate key
Collectors toMap Duplicate Key | Java Developer Central
December 2, 2019 - I found the changes they’ve made for this to be interesting and hence I’ve included it in the last part of this post i.e., I will also show how the Collectors.toMap library code was in Java 8 and how it was changed to support a better (and a clear) exception message for the duplicate keys case. ... When Collectors.toMap would throw an IllegalStateException and the messaging difference between Java 8 and Java 9+. ... If you are here to just learn how to fix the error, feel free to ignore the last part of this post. Before moving on, here is a quick recap about the toMap methods. There are three overloads of the toMap methods. ... public static <T, K, U> Collector<T, ?, Map<K,U>> toMap(Function<? super T, ?
🌐
Medium
medium.com › @AlexanderObregon › javas-collectors-tomap-method-explained-f95999d4ebe6
Java’s Collectors.toMap() Method Explained | Medium
October 20, 2024 - For example, you can filter out duplicates before mapping or use a strategy that generates unique keys dynamically (e.g., by appending a suffix or using a composite key). Here’s an example where we generate unique keys by appending a unique suffix for each employee with a duplicate ID: List<Employee> employees = List.of( new Employee(1, "Kaitlyn"), new Employee(1, "David"), // Duplicate ID new Employee(2, "Alex"), new Employee(3, "Chuck") ); AtomicInteger counter = new AtomicInteger(1); Map<String, String> employeeMap = employees.stream() .collect(Collectors.toMap( e -> e.getId() + "-" + counter.getAndIncrement(), // Unique key with suffix Employee::getName ));