Make variables final where possible

This commit is contained in:
niklas committed 2026-11-12 13:49:40 +01:00
1 parent e1c93881af
commit 51c12cc0ea
15 files changed
+148 -146

No files matched your search

@@ -19,36 +19,38 @@ public class KNearestNeighbour implements MachineLearning
private final int k;
public KNearestNeighbour(DistanceFunction distanceFunction)
public KNearestNeighbour(final DistanceFunction distanceFunction)
{
this(distanceFunction, 1);
}
public KNearestNeighbour(DistanceFunction distanceFunction, int k)
public KNearestNeighbour(final DistanceFunction distanceFunction, final int k)
{
this.distanceFunction = distanceFunction;
this.k = k;
}
public void learn(List<Vector> positives, List<Vector> negatives)
@Override
public void learn(final List<Vector> positives, final List<Vector> negatives)
{
this.positives = positives;
this.negatives = negatives;
}
public DataClass classify(Vector toClassify)
@Override
public DataClass classify(final Vector toClassify)
{
var nearestNeighbours = this.nearestNeighbours(
final var nearestNeighbours = this.nearestNeighbours(
Stream.concat(this.positives.stream(), this.negatives.stream())
.collect(Collectors.toList()),
toClassify
);
var positivesWithNearestNeighboursAmount = nearestNeighbours.stream()
final var positivesWithNearestNeighboursAmount = nearestNeighbours.stream()
.filter(this.positives::contains)
.count();
var negativesWithNearestNeighboursAmount = nearestNeighbours.stream()
final var negativesWithNearestNeighboursAmount = nearestNeighbours.stream()
.filter(this.negatives::contains)
.count();
@@ -64,7 +66,7 @@ public class KNearestNeighbour implements MachineLearning
return new Random().nextBoolean() ? DataClass.POSITIVE : DataClass.NEGATIVE;
}
private List<Vector> nearestNeighbours(List<Vector> vectors, Vector vector)
private List<Vector> nearestNeighbours(final List<Vector> vectors, final Vector vector)
{
return vectors.parallelStream()
.map(v -> Map.entry(this.distanceFunction.distance(v, vector), v))