Stream流
- Collection体系集合:可以使用默认方法stream()生成流,
- Map体系集合:先把Map转成Set集合,间接生成流。
- 数组:通过Arrays工具类的stream方法
- 同种数据类型的,通过Stream接口的静态方法of 生成流
//Collection体系的集合可以使用默认方法stream()生成流
List<String> list = new ArrayList<String>();
Stream<String> listStream = list.stream();
Set<String> set = new HashSet<String>();
Stream<String> setStream = set.stream();
//Map体系的集合间接的生成流
Map<String,Integer> map = new HashMap<String, Integer>();
Stream<String> keyStream = map.keySet().stream();
Stream<Integer> valueStream = map.values().stream();
Stream<Map.Entry<String, Integer>> entryStream = map.entrySet().stream();
//数组可以通过Arrays中的静态方法stream生成流
String[] strArray = {"hello","world","java"};
Stream<String> strArrayStream = Arrays.stream(strArray);
//同种数据类型的多个数据可以通过Stream接口的静态方法of(T... values)生成流
Stream<String> strArrayStream2 = Stream.of("hello","world", "java");
Stream<Integer> intStream = Stream.of(10, 20, 30);
流的中间操作方法
| 方法名 | 说明 |
|---|---|
| Stream filter(Predicate predicate) | 用于对流中的数据进行过滤 |
| Stream limit(long maxSize) | 返回此流中的元素组成的流,截取前指定参数个数的数据 |
| Stream skip(long n) | 跳过指定参数个数的数据,返回由该流的剩余元素组成的流 |
| static Stream concat(Stream a, Stream b) | 合并a和b两个流为一个流 |
| Stream distinct() | 返回由该流的不同元素(根据Object.equals(Object) )组成的流 |
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("aaa");
arrayList.add("bbb");
arrayList.add("ccc");
arrayList.add("abc");
arrayList.add("acd");
arrayList.add("abd");
//筛选首字母为a的元素,
arrayList.stream().filter((s)->s.startsWith("a")).forEach(System.out::println);
System.out.println("=============");
//返回前3个
arrayList.stream().limit(3).forEach(System.out::println);
System.out.println("=============");
//跳过前3个
arrayList.stream().skip(3).forEach(System.out::println);
System.out.println("=============");
//创建第二个集合
ArrayList<String> arrayList1 = new ArrayList<>();
arrayList.add("aaa");
arrayList.add("bbb");
arrayList.add("ccc");
//两个流合并
Stream<String> concat = Stream.concat(arrayList.stream(), arrayList1.stream());
System.out.println("===============");
//去重
concat.distinct().forEach(System.out::println);
流的终结操作方法
| 方法名 | 说明 |
|---|---|
| void forEach(Consumer action) | 对此流的每个元素执行操作 |
| long count() | 返回此流中的元素数 |
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("aaa");
arrayList.add("bbb");
arrayList.add("ccc");
arrayList.add("abc");
arrayList.add("acd");
arrayList.add("abd");
//forEach对每个元素执行操作
arrayList.stream().forEach(s -> System.out.println(s));
//统计元素数
long a = arrayList.stream().count();
System.out.println(a);
Stream收集操作
把流中的数据收集到集合中
方法
| 方法名 | 说明 |
|---|---|
| R collect(Collector collector) | 把结果收集到集合中 |
| 方法名 | 说明 |
|---|---|
| public static Collector toList() | 把元素收集到List集合中 |
| public static Collector toSet() | 把元素收集到Set集合中 |
| public static Collector toMap(Function keyMapper,Function valueMapper) | 把元素收集到Map集合中 |
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("aaa");
arrayList.add("bbb");
arrayList.add("ccc");
arrayList.add("abc");
arrayList.add("acd");
arrayList.add("abd");
//截取前四个的流收集到集合中。
List<String> collect = arrayList.stream().limit(4).collect(Collectors.toList());
//打印集合
System.out.println(collect);

本文详细介绍了Java中的Stream API,包括从Collection、Map和数组生成流的方法。此外,还探讨了流的中间操作,如filter、limit、skip和concat等,以及终结操作,如forEach、count和collect。示例代码展示了如何使用这些操作筛选、截取、合并和统计流中的元素,并将流收集到集合中。通过这些操作,可以更高效地处理数据集合。


被折叠的 条评论
为什么被折叠?



