You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by zr...@apache.org on 2026/06/08 01:51:14 UTC
(dubbo) branch 3.3 updated: Improve type safety of CollectionUtils.toTreeSet (#16306)
This is an automated email from the ASF dual-hosted git repository.
zrlw pushed a commit to branch 3.3
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/3.3 by this push:
new 8cce1548e6 Improve type safety of CollectionUtils.toTreeSet (#16306)
8cce1548e6 is described below
commit 8cce1548e69583fdc2df7361918ed714ecad23ac
Author: shinyruo <16...@qq.com>
AuthorDate: Mon Jun 8 09:51:08 2026 +0800
Improve type safety of CollectionUtils.toTreeSet (#16306)
* Improve type safety of CollectionUtils.toTreeSet
Fixes #16297
* Add null coverage for toTreeSet comparator overload
---
.../apache/dubbo/common/utils/CollectionUtils.java | 11 +++++-
.../dubbo/common/utils/CollectionUtilsTest.java | 43 ++++++++++++++++++++++
2 files changed, 53 insertions(+), 1 deletion(-)
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CollectionUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CollectionUtils.java
index 9544dbeeec..3817a1f9de 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CollectionUtils.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CollectionUtils.java
@@ -419,7 +419,7 @@ public class CollectionUtils {
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;
}
@@ -429,6 +429,15 @@ public class CollectionUtils {
return set;
}
+ public static <T> Set<T> toTreeSet(Set<T> set, Comparator<? super T> comparator) {
+ if (set == null) {
+ 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));
}
diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/CollectionUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/CollectionUtilsTest.java
index be48654d2d..20d801a367 100644
--- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/CollectionUtilsTest.java
+++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/CollectionUtilsTest.java
@@ -21,12 +21,14 @@ import org.apache.dubbo.config.ProtocolConfig;
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;
@@ -255,6 +257,34 @@ class CollectionUtilsTest {
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;
@@ -279,4 +309,17 @@ class CollectionUtilsTest {
super(name);
}
}
+
+ private static class PriorityItem {
+
+ private final int priority;
+
+ private PriorityItem(int priority) {
+ this.priority = priority;
+ }
+
+ private int getPriority() {
+ return priority;
+ }
+ }
}