JavaStream流体系
hiriki概述
- 得益于Lambda所带来的函数式编程
- 用于简化集合和数组操作的API
Stream流式思想的核心
- 得到集合或者数组的Stream流(相当于一根传送带)
- 把元素放上去
- 利用Stream流简化的API方便的操作元素
Tips:Stream只能使用一次
Stream流的三类方法
- 获取Stream流
- 中间方法
- 流水线上的操作。一次操作完毕之后,还可以进行其他操作(支持链式编程)
- 终结方法
- 一个Stream流只能有一个终结方法,是流水线上的最后一个操作
- count() forEach() 都是终结方法
集合获取Stream流
使用Collection接口中的默认方法stream()生成流
名称 |
说明 |
default Stream stream() |
获取当前集合对象的Stream流 |
数组获取获取Stream流
名称 |
说明 |
public static Stream stream(T[] array ) |
获取当前数组的Stream流 |
public static Stream of(T… values ) |
获取当前数组/可变数据的Stream流 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| package com.test;
import com.sun.xml.internal.ws.fault.SOAPFaultBuilder;
import java.util.*; import java.util.stream.Stream;
public class StreamDemo { public static void main(String[] args) {
List<String> list = new ArrayList<>(); Stream<String> ListStream = list.stream();
Map<String,Integer> map = new HashMap<>();
Stream<String> keyStream = map.keySet().stream();
Stream<Integer> valueStream = map.values().stream(); Stream<Map.Entry<String, Integer>> keyAndValueStream= map.entrySet().stream();
String[] names = {"JOJO","Ray"}; Stream<String> ArrayStream = Arrays.stream(names); Stream<String> ArrayStream2 = Stream.of(names); } }
|
Stream流的常用API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| package com.test;
import javax.jws.soap.SOAPBinding; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.stream.Stream;
public class StreamTest { public static void main(String[] args) { List<String> names = new ArrayList<>(); Collections.addAll(names,"张三丰","张无忌","周芷若","赵敏","张靖"); System.out.println(names);
List<String> ZhangList = new ArrayList<>(); for (String name:names) { if(name.startsWith("张")){ ZhangList.add(name); } } System.out.println(ZhangList);
List<String> ZhangThreeList = new ArrayList<>(); for (String name:ZhangList) { if(name.length() == 3){ ZhangThreeList.add(name); } } System.out.println(ZhangThreeList);
names.stream() .filter(name -> name.startsWith("张")) .filter(name -> name.length() == 3) .forEach(System.out::println);
names.stream().filter(name -> name.startsWith("张")).limit(2).forEach(System.out::println);
names.stream().filter(name -> name.startsWith("张")).skip(2).forEach(System.out::println);
long count = names.stream().filter(name -> name.length() == 3).count(); System.out.println(count);
names.stream().map(name->"smart"+name).forEach(System.out::println);
names.stream().map(User::new).forEach(user -> System.out.println(user.getName()));
Stream<String> stringStream = names.stream().filter(name -> name.startsWith("张")); Stream<String> stringStream1 = Stream.of("Java1", "Java2"); Stream<String> concat = Stream.concat(stringStream, stringStream1); concat.forEach(System.out::println); } }
|
Stream流收集
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| package com.test;
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream;
public class StreamDemo2 { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("张三丰"); list.add("张无忌"); list.add("周芷若"); list.add("郭靖"); list.add("张流");
Stream<String> zhangStream = list.stream().filter(name -> name.startsWith("张")); List<String> zhangList = zhangStream.collect(Collectors.toList()); System.out.println(zhangList);
Set<String> set = list.stream().filter(name -> name.length() == 3).collect(Collectors.toSet()); System.out.println(set);
String[] array = list.stream().filter(name -> name.startsWith("张")).toArray(String[]::new); System.out.println(Arrays.toString(array)); } }
|