diff --git a/vavr/src/main/java/io/vavr/collection/TreeSet.java b/vavr/src/main/java/io/vavr/collection/TreeSet.java index 0fc37813d5..4c20bf13bd 100644 --- a/vavr/src/main/java/io/vavr/collection/TreeSet.java +++ b/vavr/src/main/java/io/vavr/collection/TreeSet.java @@ -192,7 +192,6 @@ public static > TreeSet fill(int n, Supplier< return fill(Comparators.naturalComparator(), n, s); } - @SuppressWarnings("unchecked") public static > TreeSet ofAll(Iterable values) { return ofAll(Comparators.naturalComparator(), values); } @@ -201,10 +200,10 @@ public static > TreeSet ofAll(Iterable TreeSet ofAll(Comparator comparator, Iterable values) { Objects.requireNonNull(comparator, "comparator is null"); Objects.requireNonNull(values, "values is null"); - if (values instanceof TreeSet && ((TreeSet) values).comparator() == comparator) { + if (values instanceof TreeSet && ((TreeSet) values).comparator() == comparator) { return (TreeSet) values; } else { - return values.iterator().hasNext() ? new TreeSet<>(RedBlackTree.ofAll(comparator, values)) : (TreeSet) empty(); + return values.iterator().hasNext() ? new TreeSet<>(RedBlackTree.ofAll(comparator, values)) : empty(comparator); } } diff --git a/vavr/src/test/java/io/vavr/collection/TreeSetTest.java b/vavr/src/test/java/io/vavr/collection/TreeSetTest.java index 8128c1f707..b78719910a 100644 --- a/vavr/src/test/java/io/vavr/collection/TreeSetTest.java +++ b/vavr/src/test/java/io/vavr/collection/TreeSetTest.java @@ -236,6 +236,14 @@ public void shouldCollectNonEmpty() { // -- construct + @Test + public void shouldConstructEmptySetWithExplicitComparator() { + final TreeSet ts = TreeSet. of(Comparators.naturalComparator() + .reversed()) + .addAll(Array.ofAll(1, 2, 3)); + assertThat(ts.toArray()).isEqualTo(Array.of(3, 2, 1)); + } + @Test public void shouldConstructStreamFromEmptyJavaStream() { final TreeSet actual = ofJavaStream(java.util.stream.Stream.empty()); @@ -282,6 +290,17 @@ public void shouldKeepComparator() { final List expected = List.of(3, 2, 1); assertThat(actual).isEqualTo(expected); } + + // -- removeAll + + @Test + public void shouldKeepComparatorOnRemoveAll() { + final TreeSet ts = TreeSet.of(Comparators.naturalComparator() + .reversed(), 1, 2, 3) + .removeAll(Array.ofAll(1, 2, 3)) + .addAll(Array.ofAll(4, 5, 6)); + assertThat(ts.toArray()).isEqualTo(Array.of(6, 5, 4)); + } // -- diff