求距离原点最远的点的距离

传统方法

/**
 * @author Shixinxin
 */
public class PointListTest {
    public static void main(String[] args) {
        List intList = Arrays.asList(1, 2, 3, 4, 5);

        List pointList = new ArrayList<>();
        for (Integer i : intList) {
            pointList.add(new Point(i % 3, i));
        }
        double maxDistance = Double.MIN_VALUE;
        for (Point p : pointList) {
            maxDistance = Math.max(maxDistance, p.distance(0, 0));
        }
        System.out.println(maxDistance);
    }
}

Lambda方法

/**
 * @author Shixinxin
 */
public class PointStream {
    public static void main(String[] args) {
        List intList = Arrays.asList(1, 2, 3, 4, 5);

        Double maxDistance = intList.stream()
                .map(i -> new Point(i % 3, i / 3))
                .max(comparing(p -> p.distance(0, 0)))
                .map(p -> p.distance(0, 0))
                .orElse(Double.MIN_VALUE);
        System.out.println(maxDistance);
    }
}

你可能感兴趣的:(求距离原点最远的点的距离)