Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ public static <T> T first(List<T> values) {
return values.get(0);
}

public static <T> Set<T> toTreeSet(Set<T> set) {
public static <T extends Comparable<? super T>> Set<T> toTreeSet(Set<T> set) {
if (isEmpty(set)) {
return set;
}
Expand All @@ -429,6 +429,15 @@ public static <T> Set<T> toTreeSet(Set<T> set) {
return set;
}

public static <T> Set<T> toTreeSet(Set<T> set, Comparator<? super T> comparator) {
if (set == null) {
Comment thread
skt-shinyruo marked this conversation as resolved.
return set;
}
Set<T> treeSet = new TreeSet<>(comparator);
treeSet.addAll(set);
return treeSet;
}

public static <T> Set<T> newHashSet(int expectedSize) {
return new HashSet<>(capacity(expectedSize));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -255,6 +257,34 @@ void sortShouldSupportComparableSuperType() {
assertEquals("b", sorted.get(1).getName());
}

@Test
void toTreeSetShouldSupportExplicitComparator() {
Set<PriorityItem> items = new LinkedHashSet<>(Arrays.asList(new PriorityItem(3), new PriorityItem(1)));

Set<PriorityItem> sorted = CollectionUtils.toTreeSet(items, Comparator.comparingInt(PriorityItem::getPriority));

List<PriorityItem> sortedList = new ArrayList<>(sorted);
assertEquals(1, sortedList.get(0).getPriority());
assertEquals(3, sortedList.get(1).getPriority());
}

@Test
void toTreeSetShouldPreserveComparatorForEmptyInput() {
Comparator<PriorityItem> comparator = Comparator.comparingInt(PriorityItem::getPriority);

Set<PriorityItem> sorted = CollectionUtils.toTreeSet(new LinkedHashSet<>(), comparator);

assertTrue(sorted instanceof TreeSet);
Assertions.assertSame(comparator, ((TreeSet<PriorityItem>) sorted).comparator());
}

@Test
void toTreeSetShouldReturnNullForNullInputWithComparator() {
Comparator<PriorityItem> comparator = Comparator.comparingInt(PriorityItem::getPriority);

assertNull(CollectionUtils.toTreeSet(null, comparator));
}

private static class Person implements Comparable<Person> {

private final String name;
Expand All @@ -279,4 +309,17 @@ private Student(String name) {
super(name);
}
}

private static class PriorityItem {

private final int priority;

private PriorityItem(int priority) {
this.priority = priority;
}

private int getPriority() {
return priority;
}
}
}
Loading