有没有办法从 java 中的 LocalDate 计算最小值?
Is there a way to calculate the smallest value from a LocalDate in java?
我正在 java 完成一项作业,其中我有 Prisoner
class,其中有犯罪日期 (dd-MM-yyyy) localDate 格式、姓名和入狱年限.我还有 Cell
class 我必须在牢房中添加囚犯。
我必须创建一个方法来显示哪个 Prisoner
应该首先从 Cell
中释放。
不幸的是,我不知道该怎么做。我已经制作了一个方法来在牢房中添加囚犯并且我使用了HashSet但是我不知道如何计算应该先释放谁。
这是我添加囚犯的代码
public Boolean addPrisoner(Prisoner prisoner) {
if (this.prisonerList.size() <= this.cellSize) {
return this.prisonerList.add(prisoner);
}
return false;
}
让您的囚犯计算他或她自己的释放日期(在现实生活中,这可能会导致计算欺诈性释放日期,但只要您编写该方法,在 Java 程序中就可以了)。例如:
public class Prisoner {
private LocalDate dateOfOffence;
private int yearsOfSentence;
public LocalDate getReleaseDate() {
return dateOfOffence.plusYears(yearsOfSentence);
}
}
现在Collections.min
可以找到最早释放日期的犯人:
Prisoner nextPrisonerToBeReleased = Collections.min(
prisonerList, Comparator.comparing(Prisoner::getReleaseDate));
我正在 java 完成一项作业,其中我有 Prisoner
class,其中有犯罪日期 (dd-MM-yyyy) localDate 格式、姓名和入狱年限.我还有 Cell
class 我必须在牢房中添加囚犯。
我必须创建一个方法来显示哪个 Prisoner
应该首先从 Cell
中释放。
不幸的是,我不知道该怎么做。我已经制作了一个方法来在牢房中添加囚犯并且我使用了HashSet但是我不知道如何计算应该先释放谁。
这是我添加囚犯的代码
public Boolean addPrisoner(Prisoner prisoner) {
if (this.prisonerList.size() <= this.cellSize) {
return this.prisonerList.add(prisoner);
}
return false;
}
让您的囚犯计算他或她自己的释放日期(在现实生活中,这可能会导致计算欺诈性释放日期,但只要您编写该方法,在 Java 程序中就可以了)。例如:
public class Prisoner {
private LocalDate dateOfOffence;
private int yearsOfSentence;
public LocalDate getReleaseDate() {
return dateOfOffence.plusYears(yearsOfSentence);
}
}
现在Collections.min
可以找到最早释放日期的犯人:
Prisoner nextPrisonerToBeReleased = Collections.min(
prisonerList, Comparator.comparing(Prisoner::getReleaseDate));