From a91ae587a27e6f6156bb368c4e3f832695fd1d80 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Sun, 1 Sep 2024 16:12:14 -0400 Subject: [PATCH] Use Java 8's Map.getOrDefault() Javadoc --- .../commons/collections4/CollectionUtils.java | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/apache/commons/collections4/CollectionUtils.java b/src/main/java/org/apache/commons/collections4/CollectionUtils.java index cf7d7c984..b5feffd72 100644 --- a/src/main/java/org/apache/commons/collections4/CollectionUtils.java +++ b/src/main/java/org/apache/commons/collections4/CollectionUtils.java @@ -63,6 +63,8 @@ public class CollectionUtils { */ private static class CardinalityHelper { + private static final Integer ZERO = Integer.valueOf(0); + /** Contains the cardinality for each object in collection A. */ final Map cardinalityA; @@ -81,28 +83,26 @@ public class CollectionUtils { /** * Returns the frequency of this object in collection A. - * @param obj the object + * + * @param key the key whose associated frequency is to be returned. * @return the frequency of the object in collection A */ - public int freqA(final Object obj) { - return getFreq(obj, cardinalityA); + public int freqA(final Object key) { + return getFreq(key, cardinalityA); } /** * Returns the frequency of this object in collection B. - * @param obj the object + * + * @param key the key whose associated frequency is to be returned. * @return the frequency of the object in collection B */ - public int freqB(final Object obj) { - return getFreq(obj, cardinalityB); + public int freqB(final Object key) { + return getFreq(key, cardinalityB); } - private int getFreq(final Object obj, final Map freqMap) { - final Integer count = freqMap.get(obj); - if (count != null) { - return count.intValue(); - } - return 0; + private int getFreq(final Object key, final Map freqMap) { + return freqMap.getOrDefault(key, ZERO).intValue(); } /**