Use stream API in place of explicit loop.
Closes #154.
diff --git a/src/main/java/org/apache/commons/math4/fitting/AbstractCurveFitter.java b/src/main/java/org/apache/commons/math4/fitting/AbstractCurveFitter.java
index 32aebc7..7290ca7 100644
--- a/src/main/java/org/apache/commons/math4/fitting/AbstractCurveFitter.java
+++ b/src/main/java/org/apache/commons/math4/fitting/AbstractCurveFitter.java
@@ -16,6 +16,7 @@
*/
package org.apache.commons.math4.fitting;
+import java.util.Arrays;
import java.util.Collection;
import org.apache.commons.math4.analysis.MultivariateMatrixFunction;
@@ -101,13 +102,7 @@
public TheoreticalValuesFunction(final ParametricUnivariateFunction f,
final Collection<WeightedObservedPoint> observations) {
this.f = f;
-
- final int len = observations.size();
- this.points = new double[len];
- int i = 0;
- for (WeightedObservedPoint obs : observations) {
- this.points[i++] = obs.getX();
- }
+ this.points = observations.stream().mapToDouble(WeightedObservedPoint::getX).toArray();
}
/**
@@ -118,13 +113,7 @@
/** {@inheritDoc} */
@Override
public double[] value(double[] p) {
- final int len = points.length;
- final double[] values = new double[len];
- for (int i = 0; i < len; i++) {
- values[i] = f.value(points[i], p);
- }
-
- return values;
+ return Arrays.stream(points).map(point -> f.value(point, p)).toArray();
}
};
}
diff --git a/src/main/java/org/apache/commons/math4/fitting/leastsquares/DifferentiatorVectorMultivariateJacobianFunction.java b/src/main/java/org/apache/commons/math4/fitting/leastsquares/DifferentiatorVectorMultivariateJacobianFunction.java
index bc3207f..c912ee5 100644
--- a/src/main/java/org/apache/commons/math4/fitting/leastsquares/DifferentiatorVectorMultivariateJacobianFunction.java
+++ b/src/main/java/org/apache/commons/math4/fitting/leastsquares/DifferentiatorVectorMultivariateJacobianFunction.java
@@ -26,6 +26,8 @@
import org.apache.commons.math4.linear.RealVector;
import org.apache.commons.math4.util.Pair;
+import java.util.Arrays;
+
/**
* A MultivariateJacobianFunction (a thing that requires a derivative)
* combined with the thing that can find derivatives.
@@ -88,10 +90,6 @@
DerivativeStructure[] derivatives = differentiator
.differentiate(univariateVectorFunction)
.value(new DerivativeStructure(1, 1, 0, atParameterValue));
- double[] derivativesOut = new double[derivatives.length];
- for(int index=0;index<derivatives.length;index++) {
- derivativesOut[index] = derivatives[index].getPartialDerivative(1);
- }
- return derivativesOut;
+ return Arrays.stream(derivatives).mapToDouble(derivative -> derivative.getPartialDerivative(1)).toArray();
}
}
diff --git a/src/main/java/org/apache/commons/math4/ml/clustering/DBSCANClusterer.java b/src/main/java/org/apache/commons/math4/ml/clustering/DBSCANClusterer.java
index e065eeb..5a92422 100644
--- a/src/main/java/org/apache/commons/math4/ml/clustering/DBSCANClusterer.java
+++ b/src/main/java/org/apache/commons/math4/ml/clustering/DBSCANClusterer.java
@@ -23,6 +23,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.stream.Collectors;
import org.apache.commons.math4.exception.NotPositiveException;
import org.apache.commons.math4.ml.distance.DistanceMeasure;
@@ -200,13 +201,8 @@
* @return the List of neighbors
*/
private List<T> getNeighbors(final T point, final Collection<T> points) {
- final List<T> neighbors = new ArrayList<>();
- for (final T neighbor : points) {
- if (point != neighbor && distance(neighbor, point) <= eps) {
- neighbors.add(neighbor);
- }
- }
- return neighbors;
+ return points.stream().filter(neighbor -> point != neighbor && distance(neighbor, point) <= eps)
+ .collect(Collectors.toList());
}
/**
@@ -218,11 +214,7 @@
*/
private List<T> merge(final List<T> one, final List<T> two) {
final Set<T> oneSet = new HashSet<>(one);
- for (T item : two) {
- if (!oneSet.contains(item)) {
- one.add(item);
- }
- }
+ two.stream().filter(item -> !oneSet.contains(item)).forEach(one::add);
return one;
}
}
diff --git a/src/main/java/org/apache/commons/math4/ml/neuralnet/Network.java b/src/main/java/org/apache/commons/math4/ml/neuralnet/Network.java
index 6da635e..040963b 100644
--- a/src/main/java/org/apache/commons/math4/ml/neuralnet/Network.java
+++ b/src/main/java/org/apache/commons/math4/ml/neuralnet/Network.java
@@ -31,6 +31,7 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
+import java.util.stream.Collectors;
import org.apache.commons.math4.exception.DimensionMismatchException;
import org.apache.commons.math4.exception.MathIllegalStateException;
@@ -216,12 +217,8 @@
* this network.
*/
public void deleteNeuron(Neuron neuron) {
- final Collection<Neuron> neighbours = getNeighbours(neuron);
-
// Delete links to from neighbours.
- for (Neuron n : neighbours) {
- deleteLink(n, neuron);
- }
+ getNeighbours(neuron).forEach(neighbour -> deleteLink(neighbour, neuron));
// Remove neuron.
neuronMap.remove(neuron.getIdentifier());
@@ -357,22 +354,13 @@
public Collection<Neuron> getNeighbours(Iterable<Neuron> neurons,
Iterable<Neuron> exclude) {
final Set<Long> idList = new HashSet<>();
+ neurons.forEach(n -> idList.addAll(linkMap.get(n.getIdentifier())));
- for (Neuron n : neurons) {
- idList.addAll(linkMap.get(n.getIdentifier()));
- }
if (exclude != null) {
- for (Neuron n : exclude) {
- idList.remove(n.getIdentifier());
- }
+ exclude.forEach(n -> idList.remove(n.getIdentifier()));
}
- final List<Neuron> neuronList = new ArrayList<>();
- for (Long id : idList) {
- neuronList.add(getNeuron(id));
- }
-
- return neuronList;
+ return idList.stream().map(this::getNeuron).collect(Collectors.toList());
}
/**
diff --git a/src/main/java/org/apache/commons/math4/stat/StatUtils.java b/src/main/java/org/apache/commons/math4/stat/StatUtils.java
index 0ab56c7..e736741 100644
--- a/src/main/java/org/apache/commons/math4/stat/StatUtils.java
+++ b/src/main/java/org/apache/commons/math4/stat/StatUtils.java
@@ -850,12 +850,7 @@
}
List<Double> list = freq.getMode();
// Convert the list to an array of primitive double
- double[] modes = new double[list.size()];
- int i = 0;
- for(Double c : list) {
- modes[i++] = c.doubleValue();
- }
- return modes;
+ return list.stream().mapToDouble(Double::doubleValue).toArray();
}
}