Java Reference
In-Depth Information
Figure 5.4. Skipping elements in a stream
List<Dish> dishes = menu.stream()
.filter(d -> d.getCalories() > 300)
.skip(2)
.collect(toList());
Put what you've learned in this section into practice with Quiz 5.1 before we move to mapping
operations.
Quiz 5.1: Filtering
How would you use streams to filter the first two meat dishes?
Answer:
You can solve this problem by composing the methods filter and limit together and using
collect(toList()) to convert the stream into a list as follows:
List<Dish> dishes =
menu.stream()
.filter(d -> d.getType() == Dish.Type.MEAT)
Search WWH ::




Custom Search