You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2022/12/02 15:10:20 UTC
[commons-math] 05/05: Use a LinkedHashMap to respect the order of the input data
This is an automated email from the ASF dual-hosted git repository.
aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-math.git
commit ab9b0b0c2d0913e7f2a894650255564a40b05277
Author: aherbert <ah...@apache.org>
AuthorDate: Fri Dec 2 15:08:03 2022 +0000
Use a LinkedHashMap to respect the order of the input data
Note: The int/real enumerated distributions do not specify that the
input must be sorted. This change supports the legacy behaviour enforced
by the unit test which is assuming input values are processed in
encounter order (which happens to be sorted).
Given int and double have a natural ordering it may be better to update
the distribution to sort input values. However this would contradict the
enumerated distribution which respects the input order of the list used
in the constructor.
---
.../math4/legacy/distribution/EnumeratedIntegerDistribution.java | 6 +++---
.../math4/legacy/distribution/EnumeratedRealDistribution.java | 4 ++--
2 files changed, 5 insertions(+), 5 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 010e82e86..0e0cf1b83 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
@@ -17,7 +17,7 @@
package org.apache.commons.math4.legacy.distribution;
import java.util.ArrayList;
-import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@@ -68,7 +68,7 @@ public class EnumeratedIntegerDistribution extends AbstractIntegerDistribution {
NotFiniteNumberException,
NotANumberException {
innerDistribution = new EnumeratedDistribution<>(createDistribution(singletons,
- probabilities));
+ probabilities));
}
/**
@@ -78,7 +78,7 @@ public class EnumeratedIntegerDistribution extends AbstractIntegerDistribution {
* @param data input dataset
*/
public EnumeratedIntegerDistribution(final int[] data) {
- final Map<Integer, Integer> dataMap = new HashMap<>();
+ final Map<Integer, Integer> dataMap = new LinkedHashMap<>();
for (int value : data) {
dataMap.merge(value, 1, Integer::sum);
}
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 2d33dca8b..77385c8d4 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
@@ -17,7 +17,7 @@
package org.apache.commons.math4.legacy.distribution;
import java.util.ArrayList;
-import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@@ -80,7 +80,7 @@ public class EnumeratedRealDistribution
* @param data input dataset
*/
public EnumeratedRealDistribution(final double[] data) {
- final Map<Double, Integer> dataMap = new HashMap<>();
+ final Map<Double, Integer> dataMap = new LinkedHashMap<>();
for (double value : data) {
dataMap.merge(value, 1, Integer::sum);
}