Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@
<version>5.6.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>32.1.3-jre</version>
</dependency>
</dependencies>

<build>
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/com/github/hcsp/polymorphism/Point.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package com.github.hcsp.polymorphism;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
Expand All @@ -8,6 +7,7 @@ public class Point {

private final int x;
private final int y;

// 代表笛卡尔坐标系中的一个点
public Point(int x, int y) {
this.x = x;
Expand All @@ -22,6 +22,7 @@ public int getY() {
return y;
}


@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down Expand Up @@ -53,7 +54,16 @@ public String toString() {

// 按照先x再y,从小到大的顺序排序
// 例如排序后的结果应该是 (-1, 1) (1, -1) (2, -1) (2, 0) (2, 1)
public static List<Point> sort(List<Point> points) {}
public static List<Point> sort(List<Point> 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<Point> points =
Expand All @@ -66,3 +76,4 @@ public static void main(String[] args) throws IOException {
System.out.println(Point.sort(points));
}
}