#313 Finding the First Order and Sorting Deliveries

Companies:
Aston
Themes:
JavaStreams & LambdasPractice

Implement the methods:

  • The findFirstOrder method accepts a list of orders and must return the first / earliest order in the list according to the DeliveryDate field. If the list is empty, the method must return an order created by its default constructor (“empty order”).
  • The method for printing delivery addresses also uses a list of orders and must print addresses sorted by delivery date from earliest to latest, without duplicates. Each address must be printed on a new line. If the list contains duplicates, only the first order should be printed to avoid visiting the same address repeatedly. Return the sorted collection.
public class DeliveryOrderStream {

    public static DeliveryOrder findFirstOrder(List<DeliveryOrder> orders) {
        return null;
    }

    public static Collection<DeliveryOrder> printAddressesToDeliver(Collection<DeliveryOrder> orders) {
        return null;
    }

    public static void main(String[] args) {
        List<DeliveryOrder> orders = List.of(
                new DeliveryOrder(1L, "112 Mammoth Street, Colorado Springs, CO 80911", LocalDate.of(2021, 9, 3)),
                new DeliveryOrder(2L, "369 Woodside Court, Troy, NY 12180", LocalDate.of(2021, 9, 5)),
                new DeliveryOrder(3L, "837 Bowman Street, Helena, MT 59601", LocalDate.of(2021, 9, 2)),
                new DeliveryOrder(4L, "112 Mammoth Street, Colorado Springs, CO 80911", LocalDate.of(2021, 9, 3)),
                new DeliveryOrder(5L, "112 Mammoth Street, Colorado Springs, CO 80911", LocalDate.of(2021, 10, 3)),
                new DeliveryOrder(6L, "112 Mammoth Street, Colorado Springs, CO 80911", LocalDate.of(2021, 8, 3))
        );

        System.out.println(findFirstOrder(orders));
        printAddressesToDeliver(orders).forEach(System.out::println);
    }
}

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in