Question: I have two tables `tblReservation` and `tblBooking`. The `tblReservation` has an `ID` column and an Event Name column. `tblBooking` has it's own `ID` column and has the Reservation ID column as well (foreign key), and then EventStart and EventEnd columns.

Basically I am doing the following:

```
SELECT DISTINCT 
    B1.ReservationID,
    B1.EventStart,
    B1.EventEnd,
    tblReservation.EventName
FROM        
    tblBooking AS B1 
INNER JOIN
    tblReservation ON B1.ReservationID = tblReservation.ID
WHERE       
    B1.EventEnd = (SELECT MAX(B2.EventEnd)
                   FROM tblBooking AS B2
                   WHERE B2.ReservationID = B1.ReservationID)

```

I tried this subquery to only pull one Reservation ID per booking. Essentially I want these results:

```
Reservation ID     EventStart    EventEnd     EventName
1                  1/1/18        1/2/18       Event 1
2                  1/2/18        1/3/18       Event 2 
3                  1/4/18        1/6/18       Event 3

```

Instead I'm getting:

```
Reservation ID     EventStart    EventEnd     EventName
1                  1/1/18        1/1/18       Event 1
1                  1/2/18        1/2/18       Event 1
2                  1/2/18        1/2/18       Event 2
2                  1/3/18        1/3/18       Event 2 

```

Answer: 
I don't know whether this could be considered a simplification, but here's one way to do it with streams:

```
return animals.stream()
        .collect(Collectors.groupingBy(Animal::getName))
        .values()
        .stream()
        .flatMap(group -> group.stream().skip(1))
        .map(Animal::getName)
        .collect(Collectors.toList());

```
*there is always a way — still not simple, but much shorter:*

```
List<Animal> duplicates = animals.stream()
  .collect( Collectors.collectingAndThen( Collectors.groupingBy( Animal::getName ),
    map -> {
      map.entrySet().removeIf( e -> e.getValue().size() < 2 );
      return( map.values().stream().flatMap( List::stream ).collect( Collectors.toList() ) );
    } ) );

```