Java 8 Working with streams(使用流)Filtering(筛选)示例
来源:互联网 发布:那个网络射击游戏好玩 编辑:程序博客网 时间:2024/04/29 16:48
问题描述:你将如何使用流过滤头两个肉类菜肴?
答案:
Dish.java:
import java.util.*;public class Dish { private final String name; private final boolean vegetarian; private final int calories; private final Type type; public Dish(String name, boolean vegetarian, int calories, Type type) { this.name = name; this.vegetarian = vegetarian; this.calories = calories; this.type = type; } public String getName() { return name; } public boolean isVegetarian() { return vegetarian; } public int getCalories() { return calories; } public Type getType() { return type; } public enum Type { MEAT, FISH, OTHER } @Override public String toString() { return name; } public static final List<Dish> menu = Arrays.asList( new Dish("pork", false, 800, Dish.Type.MEAT), new Dish("season fruit", true, 120, Dish.Type.OTHER), new Dish("beef", false, 700, Dish.Type.MEAT), new Dish("chicken", false, 400, Dish.Type.MEAT), new Dish("french fries", true, 530, Dish.Type.OTHER), new Dish("rice", true, 350, Dish.Type.OTHER), new Dish("pizza", true, 550, Dish.Type.OTHER), new Dish("prawns", false, 400, Dish.Type.FISH), new Dish("salmon", false, 450, Dish.Type.FISH));}
Filtering.java:
import lambdasinaction.chap4.*;import java.util.stream.*;import java.util.*;import static java.util.stream.Collectors.toList;import static lambdasinaction.chap4.Dish.menu;public class Filtering{ public static void main(String...args){ //dishesSkip3.forEach(System.out::println); List<Dish> dishesSkip3 = menu.stream() .filter(d -> d.getType()==Dish.Type.MEAT) .limit(2) .collect(toList()); dishesSkip3.forEach(System.out::println); }}
阅读全文
0 0
- Java 8 Working with streams(使用流)Filtering(筛选)示例
- 使用流(Working with Streams)
- Working with Streams 使用流
- CFNetwork编程指南(二)——使用流(Working with Streams)
- Working with Streams
- Java 8 集合的 Streams 使用示例
- iOS Working with Streams 处理流 官方文档翻译(四)
- Java 8 Streams map() 示例
- Java 8 Streams filter 示例
- Java 8 Streams map 示例
- Working With The File System & Streams (1)
- Working With The File System & Streams (2)
- Working With The File System & Streams (3)
- Working With The File System & Streams (4)
- Working with Real-Time Media Streams
- Working with Strings(使用Oracle字符串)
- 协议的使用(Working with Protocols)
- Block的使用(Working with Blocks)
- web开发中遇到request.getremoteaddr() 0:0:0:0:0:0:1
- Android IPC基础概念之(序列化)介绍
- POJ-2752-Seek the Name, Seek the Fame
- Django QueryDict对象
- SpringBoot非官方教程 | 第一篇:构建第一个SpringBoot工程
- Java 8 Working with streams(使用流)Filtering(筛选)示例
- Mysql 查看索引
- 在while()/do...while()/for()/switch()环境下,对break的使用。
- saiku安装
- SQL基础语句
- tomcat启动报错-- ERROR: transport error 202: bind failed
- spring-data-jpa 下自定义命名策略
- python里使用正则表达式的句号和星号
- 简单介绍AOP两种实现方式