test.entrySet().stream() .collect( Collectors.toMap( Map.Entry::getValue, x -> x, (a, b) -> a ) ).values() .forEach(System.out::println);
Edit:
Explanation: this snippet will take a stream of records and put them in the value map for input when discarding duplicates (see javadoc for Collectors # toMap ). Then it takes the values โโof this map as a collection. The result is a collection of record elements that are different from Map.Entry::getValue .
Edit 2:
From your comments, I think I understand what you are trying to do. You use this TreeSet as the 1st list and you want the keys to be destroyed when duplicate values โโare deleted. It's right? Perhaps you can explain why you are doing this, and not just use a list.
Streams are not suitable for this approach, so it will not be very good, but here you go: Stream values, exclude duplicates, collect in a list, and then flip the list back to the map.
test.values().stream() .distinct() .collect( Collectors.collectingAndThen( Collectors.toList(), lst -> IntStream.range(0, lst.size()).boxed().collect( Collectors.toMap(i -> i + 1, i -> lst.get(i)) ) ) ).entrySet().forEach(System.out::println); output: 1=[1] 2=[2] 3=[3, 33]
Misha source share