From 9fcd3885dc3ea5dbe18d572d2c49e55c30857d63 Mon Sep 17 00:00:00 2001 From: aherbert Date: Fri, 2 Dec 2022 13:54:50 +0000 Subject: [PATCH] Use Map.merge --- .../legacy/distribution/EnumeratedIntegerDistribution.java | 6 +----- .../legacy/distribution/EnumeratedRealDistribution.java | 6 +----- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/distribution/EnumeratedIntegerDistribution.java b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/distribution/EnumeratedIntegerDistribution.java index dce14aad6..8d40da2e4 100644 --- a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/distribution/EnumeratedIntegerDistribution.java +++ b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/distribution/EnumeratedIntegerDistribution.java @@ -80,11 +80,7 @@ public class EnumeratedIntegerDistribution extends AbstractIntegerDistribution { public EnumeratedIntegerDistribution(final int[] data) { final Map dataMap = new HashMap<>(); for (int value : data) { - Integer count = dataMap.get(value); - if (count == null) { - count = 0; - } - dataMap.put(value, ++count); + dataMap.merge(value, 1, Integer::sum); } final int massPoints = dataMap.size(); final double denom = data.length; diff --git a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/distribution/EnumeratedRealDistribution.java b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/distribution/EnumeratedRealDistribution.java index 4dad1247c..36f70183b 100644 --- a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/distribution/EnumeratedRealDistribution.java +++ b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/distribution/EnumeratedRealDistribution.java @@ -82,11 +82,7 @@ public class EnumeratedRealDistribution public EnumeratedRealDistribution(final double[] data) { final Map dataMap = new HashMap<>(); for (double value : data) { - Integer count = dataMap.get(value); - if (count == null) { - count = 0; - } - dataMap.put(value, ++count); + dataMap.merge(value, 1, Integer::sum); } final int massPoints = dataMap.size(); final double denom = data.length;