2023-02-13

Java Streams: Calculate length of a path using a list of coordinates utilizing Stream functions

I have a List of Coordinates that each contain an x and y Position. The class also offers a distanceTo(Coordinate coord)-Method, which returns the distance of the xy-Position of the Coordinate object to the coord-Coordinate.

Now I want to calculate the length of a path that is represented as List path and I'm pretty sure that there must be a clever way to do this with streams. Probably via reduce, collect or mapToDouble... I tried to find a solution but was unable to. The best I could find was this similar problem: How to get length of path using java 8 streams

But the answer there, to write a whole new class only for this one seems overkill for my use case.

Is there an elegant solution for this problem that doesn't involve creating a whole new class for Distance?

Any help would be much appreciated. Thank you.

Here is a simple code example of what I'm trying to do:

import java.util.List;

public class Main {
    public static void main(String[] args) {
        Coordinate c1 = new Coordinate(0, 0);
        Coordinate c2 = new Coordinate(0, 1);
        Coordinate c3 = new Coordinate(1, 1);
        Coordinate c4 = new Coordinate(1, 2);

        List<Coordinate> path = List.of(c1, c2, c3, c4);

        // Primitive solution I use currently
        double pathLength = 0;
        for (int i = 0; i < path.size() - 1; i++) {
            pathLength += path.get(i).distanceTo(path.get(i + 1));
        }

        // Using Steams
        

    }


    public static class Coordinate {
        private final int x;
        private final int y;

        public Coordinate(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public double distanceTo(Coordinate coord) {
            return Math.hypot(x - coord.x, y - coord.y);
        }
    }
}


No comments:

Post a Comment