diff --git a/pom.xml b/pom.xml index 86a2927..3dae4a9 100644 --- a/pom.xml +++ b/pom.xml @@ -64,6 +64,11 @@ 5.6.0 test + + com.google.guava + guava + 32.1.3-jre + diff --git a/src/main/java/com/github/hcsp/polymorphism/Point.java b/src/main/java/com/github/hcsp/polymorphism/Point.java index 780e6b1..f402035 100644 --- a/src/main/java/com/github/hcsp/polymorphism/Point.java +++ b/src/main/java/com/github/hcsp/polymorphism/Point.java @@ -1,5 +1,4 @@ package com.github.hcsp.polymorphism; - import java.io.IOException; import java.util.Arrays; import java.util.List; @@ -8,6 +7,7 @@ public class Point { private final int x; private final int y; + // 代表笛卡尔坐标系中的一个点 public Point(int x, int y) { this.x = x; @@ -22,6 +22,7 @@ public int getY() { return y; } + @Override public boolean equals(Object o) { if (this == o) { @@ -53,7 +54,16 @@ public String toString() { // 按照先x再y,从小到大的顺序排序 // 例如排序后的结果应该是 (-1, 1) (1, -1) (2, -1) (2, 0) (2, 1) - public static List sort(List points) {} + public static List sort(List points) { + points.sort((p1, p2) -> { + if (p1.getX() != p2.getX()) { + return Integer.compare(p1.getX(), p2.getX()); + } else { + return Integer.compare(p1.getY(), p2.getY()); + } + }); + return points; + } public static void main(String[] args) throws IOException { List points = @@ -66,3 +76,4 @@ public static void main(String[] args) throws IOException { System.out.println(Point.sort(points)); } } +