Reputation: 7652
Pre Java 8, we have all seen code like this:
List<Pair<A, B>> someList = ....
for (Pair<A, B> item : someList) {
A leftItem = item.getLeft();
aMethodThatProcessesTypeA(leftItem);
}
Seems like it should be simple/trivial to do via a Stream:
someList.stream()
.map(i -> i.getLeft())
.???
However, now I get stuck - with Streams being lazy, I am not sure how to proceed. Can anyone enlighten me?
Upvotes: 2
Views: 211
Reputation: 8068
Try it this way:
someList.stream()
.map(Pair::getLeft)
.forEach(this::aMethodThatProcessesTypeA);
assuming the method aMethodThatProcessesTypeA()
is defined by the same instance as the method running your Java 8 Stream Code shown above.
Upvotes: 3