有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将BiPredicate传递到流以比较对象列表

只有在行程时间表不重叠的情况下,一个人才能完成行程列表。e、 g.此列表应返回true,因为日期不重叠

Journey 1: "2019-09-10 21:00" --> "2019-09-10 21:10"
Journey 2: "2019-08-11 22:10" --> "2019-08-11 22:20"
Journey 3: "2019-09-10 21:30" --> "2019-09-10 22:00"

我创建了一个谓词来检查行程时间是否重叠。我想在流中使用这个双预测。解决这个问题的正确方法是什么

public class Journey {

public static void main(String[] args) throws Exception {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("y-M-d H:m");
    ArrayList<Route> routes = new ArrayList<>();
    // This example should return true because there is no overlap between the routes.
    routes.add(new Route(simpleDateFormat.parse("2019-09-10 21:00"), simpleDateFormat.parse("2019-09-10 21:10")));
    routes.add(new Route(simpleDateFormat.parse("2019-08-11 22:10"), simpleDateFormat.parse("2019-08-11 22:20")));
    routes.add(new Route(simpleDateFormat.parse("2019-09-10 21:30"), simpleDateFormat.parse("2019-09-10 22:00")));

    boolean result = travelAllRoutes(routes);

    System.out.println(result);
}

public static boolean travelAllRoutes(List<Route> routes) {

    BiPredicate<Route, Route> predicate = (r1, r2) -> r1.getEndJourney().before(r2.getStartJourney());

    // boolean result = routes.stream(); // use predicate here
    return result;
}
}


class Route {
    private Date startJourney, endJourney;

    public Route(Date startJourney, Date endJourney) {
    this.startJourney = startJourney;
    this.endJourney = endJourney;
}

public Date getStartJourney() {
    return startJourney;
}

public void setStartJourney(Date startJourney) {
    this.startJourney = startJourney;
}

public Date getEndJourney() {
    return endJourney;
}

public void setEndJourney(Date endJourney) {
    this.endJourney = endJourney;
}
}

共 (0) 个答案