From 1264673cd60eee181904f2700466b408bdacc8cb Mon Sep 17 00:00:00 2001 From: Walter Oggioni Date: Mon, 4 Jan 2021 21:27:10 +0100 Subject: [PATCH] improved CollectionUtils TreeSet and TreeMap methods --- .../net/woggioni/jwo/CollectionUtils.java | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/main/java/net/woggioni/jwo/CollectionUtils.java b/src/main/java/net/woggioni/jwo/CollectionUtils.java index 57c45a4..a917893 100644 --- a/src/main/java/net/woggioni/jwo/CollectionUtils.java +++ b/src/main/java/net/woggioni/jwo/CollectionUtils.java @@ -47,52 +47,53 @@ public class CollectionUtils { return Stream.of(elements).collect(toUnmodifiableSet()); } - private static Collector> createTreeSetCollector(Function, Set> finalizer, Comparator comparator) { + private static Collector> + createTreeSetCollector(Function, NavigableSet> finalizer, Comparator comparator) { return Collector.of( () -> new TreeSet<>(comparator), Set::add, - (Set s1, Set s2) -> { + (NavigableSet s1, NavigableSet s2) -> { s1.addAll(s2); return s1; }, finalizer); } - private static > Collector> - createTreeSetCollector(Function, Set> finalizer) { + private static > Collector> + createTreeSetCollector(Function, NavigableSet> finalizer) { return Collector.of( TreeSet::new, Set::add, - (Set s1, Set s2) -> { + (NavigableSet s1, NavigableSet s2) -> { s1.addAll(s2); return s1; }, finalizer); } - public static > Collector> toTreeSet() { + public static > Collector> toTreeSet() { return createTreeSetCollector(Function.identity()); } - public static Collector> toTreeSet(Comparator comparator) { + public static Collector> toTreeSet(Comparator comparator) { return createTreeSetCollector(Function.identity(), comparator); } - public static Collector> toUnmodifiableTreeSet(Comparator comparator) { - return createTreeSetCollector(Collections::unmodifiableSet, comparator); + public static Collector> toUnmodifiableTreeSet(Comparator comparator) { + return createTreeSetCollector(Collections::unmodifiableNavigableSet, comparator); } - public static > Collector> toUnmodifiableTreeSet() { - return createTreeSetCollector(Collections::unmodifiableSet); + public static > Collector> toUnmodifiableTreeSet() { + return createTreeSetCollector(Collections::unmodifiableNavigableSet); } @SafeVarargs - public static Set immutableTreeSet(Comparator comparator, T... elements) { + public static NavigableSet immutableTreeSet(Comparator comparator, T... elements) { return Stream.of(elements).collect(toUnmodifiableTreeSet(comparator)); } @SafeVarargs - public static > Set immutableTreeSet(T... elements) { + public static > NavigableSet immutableTreeSet(T... elements) { return Stream.of(elements).collect(toUnmodifiableTreeSet()); } @@ -147,11 +148,11 @@ public class CollectionUtils { return toNavigableMap(() -> new TreeMap<>(comparator), keyExtractor, valueExtractor); } - public static Collector> toNavigableMap( - Supplier> constructor, + public static > Collector toNavigableMap( + Supplier constructor, Function keyExtractor, Function valueExtractor) { - BiConsumer, T> accumulator = (map, streamElement) -> { + BiConsumer accumulator = (map, streamElement) -> { map.merge(keyExtractor.apply(streamElement), valueExtractor.apply(streamElement), throwingMerger()); }; return Collector.of(