This is an automated email from the ASF dual-hosted git repository.

garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git


The following commit(s) were added to refs/heads/master by this push:
     new eedd91729 count duplicate values in ObjectUtils.median (#1715)
eedd91729 is described below

commit eedd91729833cedcb31b0fd8116869c71e57ac74
Author: alhuda <[email protected]>
AuthorDate: Thu Jun 18 21:28:28 2026 +0530

    count duplicate values in ObjectUtils.median (#1715)
---
 src/main/java/org/apache/commons/lang3/ObjectUtils.java   | 15 +++++++--------
 .../java/org/apache/commons/lang3/ObjectUtilsTest.java    |  8 ++++++++
 2 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/src/main/java/org/apache/commons/lang3/ObjectUtils.java 
b/src/main/java/org/apache/commons/lang3/ObjectUtils.java
index 93c1a3dd0..97e7e63ce 100644
--- a/src/main/java/org/apache/commons/lang3/ObjectUtils.java
+++ b/src/main/java/org/apache/commons/lang3/ObjectUtils.java
@@ -21,15 +21,14 @@
 import java.lang.reflect.Array;
 import java.time.Duration;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
 import java.util.Hashtable;
 import java.util.Map;
 import java.util.Objects;
 import java.util.Optional;
-import java.util.TreeSet;
 import java.util.function.Consumer;
 import java.util.function.Supplier;
 import java.util.stream.Stream;
@@ -1058,9 +1057,9 @@ public static <T> T median(final Comparator<T> 
comparator, final T... items) {
         Validate.notEmpty(items, "null/empty items");
         Validate.noNullElements(items);
         Objects.requireNonNull(comparator, "comparator");
-        final TreeSet<T> treeSet = new TreeSet<>(comparator);
-        Collections.addAll(treeSet, items);
-        return (T) treeSet.toArray()[(treeSet.size() - 1) / 2];
+        final T[] sorted = items.clone();
+        Arrays.sort(sorted, comparator);
+        return sorted[(sorted.length - 1) / 2];
     }
 
     /**
@@ -1077,9 +1076,9 @@ public static <T> T median(final Comparator<T> 
comparator, final T... items) {
     public static <T extends Comparable<? super T>> T median(final T... items) 
{
         Validate.notEmpty(items);
         Validate.noNullElements(items);
-        final TreeSet<T> sort = new TreeSet<>();
-        Collections.addAll(sort, items);
-        return (T) sort.toArray()[(sort.size() - 1) / 2];
+        final T[] sorted = items.clone();
+        Arrays.sort(sorted);
+        return sorted[(sorted.length - 1) / 2];
     }
 
     /**
diff --git a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java 
b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
index 51a650217..1eb355390 100644
--- a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
@@ -307,6 +307,8 @@ void testComparatorMedian() {
         assertSame(baz, ObjectUtils.median(cmp, foo, bar, baz));
         assertSame(baz, ObjectUtils.median(cmp, foo, bar, baz, blah));
         assertSame(blah, ObjectUtils.median(cmp, foo, bar, baz, blah, wah));
+        // duplicates are counted, not collapsed
+        assertSame(foo, ObjectUtils.median(cmp, foo, foo, bar));
     }
 
     @Test
@@ -736,6 +738,12 @@ void testMedian() {
                 Integer.valueOf(9)));
         assertEquals(Integer.valueOf(6),
             ObjectUtils.median(Integer.valueOf(5), Integer.valueOf(6), 
Integer.valueOf(7), Integer.valueOf(8)));
+        // duplicates are counted, not collapsed
+        assertEquals(Integer.valueOf(3),
+            ObjectUtils.median(Integer.valueOf(1), Integer.valueOf(2), 
Integer.valueOf(3), Integer.valueOf(3),
+                Integer.valueOf(3), Integer.valueOf(4)));
+        assertEquals(Integer.valueOf(5),
+            ObjectUtils.median(Integer.valueOf(5), Integer.valueOf(5), 
Integer.valueOf(5), Integer.valueOf(1)));
     }
 
     @Test

Reply via email to