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-collections.git


The following commit(s) were added to refs/heads/master by this push:
     new 75699fc1c [COLLECTIONS-878] MapUtils.invertMap() improves HashMap 
construction (#652).
75699fc1c is described below

commit 75699fc1c5e388ab06f1196cc9bebb078d30f1b5
Author: Gary Gregory <[email protected]>
AuthorDate: Wed Jun 17 22:49:44 2026 +0000

    [COLLECTIONS-878] MapUtils.invertMap() improves HashMap construction
    (#652).
    
    A version of PR #652 with some refactoring and excluding the test that
    doesn't actually test.
---
 src/changes/changes.xml                                 |  1 +
 .../java/org/apache/commons/collections4/MapUtils.java  |  6 +++++-
 .../org/apache/commons/collections4/MapUtilsTest.java   | 17 +++++++----------
 3 files changed, 13 insertions(+), 11 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 5f0b44cdd..3f18bf12d 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -51,6 +51,7 @@
     <action type="fix" dev="ggregory" due-to="Suhyeon Park, Gary Gregory" 
issue="COLLECTIONS-881">MultiMapUtils.getXXX ensure safe copy (#669).</action>
     <action type="fix" dev="ggregory" due-to="Dexter.k, Gary 
Gregory">Re-validate entries in PredicatedMap/PredicatedCollection readObject 
(#682).</action>
     <action type="fix" dev="ggregory" due-to="Ruiqi Dong, Gary Gregory" 
issue="COLLECTIONS-889">IndexedCollection.remove(Object) removes all values for 
a non-unique index key.</action>
+    <action type="fix" dev="ggregory" due-to="Maxim Safronov, Gary Gregory" 
issue="COLLECTIONS-878">MapUtils.invertMap() improves HashMap construction 
(#652).</action>
     <!-- ADD -->
     <action type="add" dev="ggregory" due-to="Gary Gregory">Add generics to 
UnmodifiableIterator for the wrapped type.</action>
     <action type="add" dev="ggregory" due-to="Gary Gregory">Add a Maven 
benchmark profile for JMH.</action>
diff --git a/src/main/java/org/apache/commons/collections4/MapUtils.java 
b/src/main/java/org/apache/commons/collections4/MapUtils.java
index 800199dfe..10b560dd5 100644
--- a/src/main/java/org/apache/commons/collections4/MapUtils.java
+++ b/src/main/java/org/apache/commons/collections4/MapUtils.java
@@ -148,6 +148,10 @@ public class MapUtils {
         return value == null ? defaultValue : value;
     }
 
+    private static int calculateHashMapCapacity(final int numMappings) {
+        return (int) Math.ceil(numMappings / 0.75d);
+    }
+
     /**
      * Prints the given map with nice line breaks.
      * <p>
@@ -1185,7 +1189,7 @@ public class MapUtils {
      */
     public static <K, V> Map<V, K> invertMap(final Map<K, V> map) {
         Objects.requireNonNull(map, "map");
-        final Map<V, K> out = new HashMap<>(map.size());
+        final Map<V, K> out = new 
HashMap<>(calculateHashMapCapacity(map.size()));
         for (final Entry<K, V> entry : map.entrySet()) {
             out.put(entry.getValue(), entry.getKey());
         }
diff --git a/src/test/java/org/apache/commons/collections4/MapUtilsTest.java 
b/src/test/java/org/apache/commons/collections4/MapUtilsTest.java
index 1d1efc76c..c047d497d 100644
--- a/src/test/java/org/apache/commons/collections4/MapUtilsTest.java
+++ b/src/test/java/org/apache/commons/collections4/MapUtilsTest.java
@@ -633,11 +633,10 @@ class MapUtilsTest {
 
     private void testInvertMap(final Map<String, String> in) {
         // setup
-        in.put("1", "A");
-        in.put("2", "B");
-        in.put("3", "C");
-        in.put("4", "D");
-        in.put("5", "E");
+        final int entryCount = 32;
+        for (int i = 1; i <= entryCount; i++) {
+            in.put(String.valueOf(i), String.valueOf((char) ('A' + i - 1)));
+        }
         final Set<String> inKeySet = new HashSet<>(in.keySet());
         final Set<String> inValSet = new HashSet<>(in.values());
         // invert
@@ -647,11 +646,9 @@ class MapUtilsTest {
         final Set<String> outValSet = new HashSet<>(out.values());
         assertEquals(inKeySet, outValSet);
         assertEquals(inValSet, outKeySet);
-        assertEquals("1", out.get("A"));
-        assertEquals("2", out.get("B"));
-        assertEquals("3", out.get("C"));
-        assertEquals("4", out.get("D"));
-        assertEquals("5", out.get("E"));
+        for (int i = 1; i <= entryCount; i++) {
+            assertEquals(String.valueOf((char) ('A' + i - 1)), 
in.get(String.valueOf(i)));
+        }
     }
 
     @Test

Reply via email to