Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using streams, lambda in java 8

I would like to optimize the below code to take an existing ArrayList and make a new list based on the below logic. Does streaming/lambda in Java 8 make it possible?

for (ShipmentTracingDTO tracing : tracings) {
    if (tracing.getDestination()) {
        newTracings.add(tracing);
        break;
    }
    newTracings.add(tracing);
}
like image 788
AhmedBakran Avatar asked Jan 10 '19 06:01

AhmedBakran


2 Answers

You can do it using IntStream.range and List.subList possibly as :

List<ShipmentTracingDTO> newTracings = tracings.subList(0, IntStream.range(0, tracings.size())
       .filter(i -> tracings.get(i).getDestination())
       .map(a -> a + 1) // to include the index of occurrence
       .findFirst()
       .orElse(tracings.size())); // else include all until last element

Note: This is just a view of tracings and hence changing the underlying list tracings would also reflect in changes in newTracings. If you want to decouple these, you can create a new ArrayList<>() from the solutions.

Edit: In comments from Holger, you could alternatively use:

List<ShipmentTracingDTO> newTracings = IntStream.rangeClosed(1, tracings.size())
        .filter(i -> tracings.get(i - 1).getDestination()) // i-1 to include this as well
        .mapToObj(i -> tracings.subList(0, i)) // Stream<List<ShipmentTracingDTO>>
        .findFirst() // Optional<List<ShipmentTracingDTO>>
        .orElse(tracings); // or else the entire list
like image 83
Naman Avatar answered Sep 17 '22 18:09

Naman


If I needed such processing I would divide it into two separate streams:

final int lastElemPosition = tracings.size() - 1;
final int firstElemWithGetDestinationTruePosition = IntStream.range(0, lastElemPosition)
    .filter(i -> tracings.get(i).getDestination())
    .findFirst()
    .orElse(lastElemPosition);
final List<ShipmentTracingDTO> desiredElements = IntStream.rangeClosed(0, firstElemWithGetDestinationTruePosition)
    .mapToObj(tracings::get)
    .collect(Collectors.toList());
like image 44
Michał Marcinkowski Avatar answered Sep 17 '22 18:09

Michał Marcinkowski