Use Map.merge

This commit is contained in:
aherbert 2022-12-02 13:54:50 +00:00
parent 7ed772e474
commit 9fcd3885dc
2 changed files with 2 additions and 10 deletions

View File

@ -80,11 +80,7 @@ public class EnumeratedIntegerDistribution extends AbstractIntegerDistribution {
public EnumeratedIntegerDistribution(final int[] data) { public EnumeratedIntegerDistribution(final int[] data) {
final Map<Integer, Integer> dataMap = new HashMap<>(); final Map<Integer, Integer> dataMap = new HashMap<>();
for (int value : data) { for (int value : data) {
Integer count = dataMap.get(value); dataMap.merge(value, 1, Integer::sum);
if (count == null) {
count = 0;
}
dataMap.put(value, ++count);
} }
final int massPoints = dataMap.size(); final int massPoints = dataMap.size();
final double denom = data.length; final double denom = data.length;

View File

@ -82,11 +82,7 @@ public class EnumeratedRealDistribution
public EnumeratedRealDistribution(final double[] data) { public EnumeratedRealDistribution(final double[] data) {
final Map<Double, Integer> dataMap = new HashMap<>(); final Map<Double, Integer> dataMap = new HashMap<>();
for (double value : data) { for (double value : data) {
Integer count = dataMap.get(value); dataMap.merge(value, 1, Integer::sum);
if (count == null) {
count = 0;
}
dataMap.put(value, ++count);
} }
final int massPoints = dataMap.size(); final int massPoints = dataMap.size();
final double denom = data.length; final double denom = data.length;