Java-Stream流

一、体验Stream流

需求:

  1. 创建一个集合,存储多个字符串元素
  2. 把集合中所有以“张”开头的元素存储到一个新的集合
  3. 把“张”开头的集合中的长度为3的元素存储到新的集合
  4. 遍历上一步得到的集合

而这样内容太复杂了,想使代码更简洁就需要用到接下来要学习的Stream流

二、Stream流的常见生成方式

2.1 Stream流的使用

生成流

通过数据源(集合,数组等)生成流

list.stream()

中间操作

一个流后面可以跟随零个或多个中间操作,其目的主要是打开流,做出某种程度的数据过滤/映射,然后返回一个新的流,交给下一个操作使用

filter()

终结操作

一个流只能有一个终结操作,当这个操作执行后,流就被使用“光”了,无法再被操作。所以这必定是流的最后一个操作

forEach()

2.2 Stream流的常见生成方式

  • Collection体系的集合可以使用默认方法stream()生成流,default Stream stream()
  • Map体系的集合间接的生成流
  • 数组可以通过Stream接口的静态方法of(T…values)生成流
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class StreamDemo3 {
public static void main(String[] args) {
//Collection体系的集合可以使用默认方法stream()生成流
List<String> list=new ArrayList<String>();
Stream<String> stream = 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();

//数组可以通过Stream接口的静态方法生成流
String[] strArr={"hello","world","yolin"};
Stream<String> sstrArrayStream1 = Stream.of(strArr);
Stream<String> strArrayStream2 = Stream.of("hello", "world", "yolin");
Stream<Integer> integerStream = Stream.of(10, 20, 30);
}
}

2.3 Stream流的常见中间操作方式

注意以下方法都在Stream接口中

操作方式一

Stream filter(Predicate predicate):用于对流中的数据进行过滤

Predicate接口中的方法

boolean test(T t):对给定的参数进行判断,返回一个布尔值

操作方式二

Stream limit(long maxSize):返回此流中的元素组成的流,截取前指定参数个数的数据

Stream skip(long n):跳过指定参数个数的数据,返回由该流的剩余元素组成的流

操作方式三

static Stream concat(Stream a,Stream b):合并a和b两个流为一个流

Stream distinct():返回由该流的不同元素(根据Object.equals(Object))组成的流

操作方式四

Stream sorted():返回由此流的元素组成的流,根据自然顺序排序

Stream sorted(Comparator comparator):返回由该流的元素组成的流,根据提供的Comparator进行排序

操作方式五

Stream map(Function mapper):返回由给定函数应用于此流的元素的结果组成的流

Function接口中的方法 R apply(T t)

IntStream mapToInt(ToIntFunction mapper):返回一个IntStream其中包含将给定函数应用于此流的元素的结果。

IntStream:表示原始int流

ToIntFunction 接口中的方法 int applyAsInt(T value)

2.4 Stream流的常见终结操作方式

void forEach(Consumer action):对此流的每个元素执行操作

Consumer接口中的方法 void accept(T t):对给定的参数执行此操作

long count():返回此流中的元素数

三、Stream流练习

现有两个ArrayList集合,分别存储6名男演员名称和6名女演员名称,要求完成如下操作

  • 男演员只要名字为3个字的前三个人
  • 女演员只要姓林的,并且不要第一个
  • 把过滤后的男演员姓名和女演员姓名合并到一起
  • 把上一步操作后的元素作为构造方法的参数创建演员对象,遍历数据

可以使用一步完成

四、Stream流的收集操作

‘对数据使用Stream流的方式操作完毕后,需要将流中的数据收集到集合中,如何操作?

Stream流的收集方法

  • R collect(Collector collector)
  • 但这个收集方法的参数是一个Collector接口

工具类Collectors提供了具体的收集方式

  • public static Collector toList():把元素收集到List集合中
  • public static Collector toSet():把元素收集到Set集合中
  • public static Collector toMap(Function keyMappe,Function valueMapper):把元素收集到Map集合中

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
public class StreamTo {
public static void main(String[] args) {
//创建List集合对象
List<String> list=new ArrayList<String>();
list.add("林青霞");
list.add("张曼玉");
list.add("柳岩");
//1.得到名字为3个字的流
Stream<String> listStream = list.stream().filter(s -> s.length() == 3);
//2.把使用stream流操作完毕的数据收集到List集合中并遍历
List<String> names = listStream.collect(Collectors.toList());
for (String s:names){
System.out.println(s);
}

//创建Set集合对象
Set<Integer> set=new HashSet<Integer>();
set.add(10);
set.add(20);
set.add(30);
set.add(33);
set.add(35);
//3.得到年龄大于25 的流
Stream<Integer> setStream = set.stream().filter(i -> i > 25);
//4.把使用Stream流操作完毕的数据收集到Set集合并遍历
Set<Integer> collect = setStream.collect(Collectors.toSet());
for(Integer i:collect){
System.out.println(i);
}

//创建字符串数组
String[] strArray={"猪头新,20","张望,28","刘升,30","王宇,25"};
//5.得到字符串中年龄大于25的流
Stream<String> stringStream = Stream.of(strArray).filter(s -> Integer.parseInt(s.split(",")[1]) > 25);
//6.把使用Stream流操作完毕的数据收集到Map集合中并遍历,字符串中的姓名作键,年龄作值
Map<String, Integer> map = stringStream.collect(Collectors.toMap(s -> s.split(",")[0], s -> Integer.parseInt(s.split(",")[1])));
Set<String> keySet = map.keySet();
for(String key:keySet){
Integer value = map.get(key);
System.out.println(key+","+value);
}
}
}

请我喝杯咖啡吧~

支付宝
微信