what would this code be in an alternative way

Viewed 75

would anyone be able to write this code in an easier way to understand

 private List<Passenger> getPassengersToRelease() {

        return passengerList.stream()
                .filter(user -> user.getDestinationFloor() == currentFloor)
                .collect(Collectors.toList());
    }

I really cant get my head around it and have been struggling on it for a few days. Thanks

5 Answers

Here is a rough translation into an old-fashioned loop:

private List<Passenger> getPassengersToRelease() {
    List<Passenger> result = new ArrayList<>();
    for (Passenger p: passengerList()) {
        if (p.getDestinationFloor() == currentFloor) {
             result.add(p);
        }
    }
    return result;
}

But a better way to understand the stream-based solution is doing is to break it down:

// give us the passengers as a stream
passengerList.stream() 

   // remove any passengers from the stream that DO NOT have the given
   // destination floor.
   .filter(user -> user.getDestinationFloor() == currentFloor)

   // put the passengers that remain into a new list.
   .collect(Collectors.toList);

The only tricky step is the filter(...) step. The stuff passed as a parameter to filter is a lambda expression. Basically, it is an inline (unnamed) function that maps from a Passenger to a boolean value. The user is the function's input variable, and the value of the expression user.getDestinationFloor() == currentFloor is its output.

So the filter works like this. For each Passenger in the stream, you call the lambda expression. This gives you either true or false. If you get true you pass on the Passenger. If you get false you drop it from the stream; i.e. it is "filtered out".

Here, I think this is the simplest way to refactor code you posted above. If I'm wrong, please correct me.

private List<Passenger> getPassengersToRelease() {

        List<Passenger> newPassengerList = new ArrayList<>();
        for(Passenger user : passengerList) {
            if(user.getDestinationFloor() == currentFloor) {
                newPassengerList.add(user);
            }
        }
        return newPassengerList;
    }

This piece of code is levereging the Stream api available since Java 8.

In short, your code goes through a List of Passenger objects, and .filter(user -> user.getDestinationFloor() == currentFloor) only filters Passenger objects that have the destinationFloor field equal with currentFloor value. After the filtering, the results are collected into a List and returned.

This:

private List<Passenger> getPassengersToRelease() 
{
    List floorPassengers;
    for( Passenger user : passengerList )
    {
        if( user.getDestinationFloor() == currentFloor )
        {
            floorPassengers.add( user );
        }
    }
    return floorPassengers;
}

Should check the syntax, I have no compiler at hand. But basically the call translates to a for each with a condition.

You can rewrite it as

private List<Passenger> getPassengersToRelease() {
    List<Passenger> list = new ArrayList<Passenger>();

    for(Passenger passenger : passengerList){
        if(passenger.getDestinationFloor() == currentFloor){
            list.add(passenger);
        }
    }
    return list;
}

I also recommend you go through Java SE 8 Streams.

Related