Stream流
1、Stream的作用
结合了Lambda表达式,简化集合、数组的操作
2、Stream流的使用步骤
- 先得到一条Stream流,并把数据放上去
- 利用Stream流中的API进行各种操作
单列集合
public class Stream01 {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<>();
Collections.addAll(arrayList,"a","b","c","d");
arrayList.stream().forEach(s -> System.out.println(s));
}
}
双列集合
注意:双列集合不能使用stream流,需要先转成单列集合
public class Stream02 {
public static void main(String[] args) {
HashMap<String, Integer> hashMap = new HashMap<>();
hashMap.put("A",1);
hashMap.put("B",2);
hashMap.put("C",3);
Set<String> strings = hashMap.keySet();
strings.stream().forEach(s -> System.out.println(hashMap.get(s)));
}
}
数组
public class Stream03 {
public static void main(String[] args) {
int[] arr = {
1,2,3,4,5,6};
Arrays.stream(arr).forEach(s -> System.out.println(s));
}
}
零散的数据
注意:需要是同种数据类型
public class Stream04 {
public static void main(String[] args) {
Stream.of(1,2,4,5,6,7).forEach(s -> System.out.println(s));
}
}
3、Stream的中间方法
注意:
- 中间方法,返回新的Stream流,原来的Stream流只能使用一次,建议使用链式编程
- 修改Stream流中的数据,不会影响原来集合或者数组中的数据
过滤 filter
public class Stream05 {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<>();
Collections.addAll(arrayList,"张三","张子","小林","张天","刘十三");
//留下张开头的,其余的过滤
arrayList.stream().filter(s -> s.startsWith("张")).forEach(s -> System.out.println(s));
}
}
获取前几个数据 limit
public class Stream05 {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<>();
Collections.addAll(arrayList,"张三","张子","小林","张天","刘十三");
//获取前几个数据 limit
arrayList.stream().limit(3).forEach(s -> System.out.println(s));
}
}
跳过前几个数据 skip
public class Stream05 {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<>();
Collections.addAll(arrayList,"张三","张子","小林","张天","刘十三");
//跳过前几个数据 skip
arrayList.stream().skip(3).forEach(s -> System.out.println(s));
}
}
去重 distinct
public class Stream06 {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<>();
Collections.addAll(arrayList,"小林","小吴","小林","小天","小吴","小沈");
arrayList.stream().distinct().forEach(s -> System.out.println(s));
}
}
合并流 concat
public class Stream07 {
public static void main(String[] args) {
ArrayList<String> arrayList01 = new ArrayList<>();
ArrayList<String> arrayList02 = new ArrayList<>();
Collections.addAll(arrayList01,"小林","小吴");
Collections.addAll(arrayList02,"小沈","小天");
Stream.concat(arrayList01.stream(),arrayList02.stream()).forEach(s -> System.out.println(s));
}
}
类型转换 map
public class Stream07 {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<>();
Collections.addAll(arrayList,"小林-15","小吴-14");
//只获取年龄
arrayList.stream().map(s -> Integer.parseInt(s.split("-")[1])).forEach(s -> System.out.println(s));
}
4、Stream的终结方法
遍历 forEach
public class Stream08 {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<>();
Collections.addAll(arrayList,"小林","小吴","小沈","小天");
arrayList.stream().forEach(s -> System.out.println(s));
}
}
count 统计
public class Stream08 {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<>();
Collections.addAll(arrayList,"小林","小吴","小沈","小天");
System.out.println(arrayList.stream().count());
}
}
存放到数组 toArray
public class Stream08 {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<>();
Collections.addAll(arrayList,"小林","小吴","小沈","小天");
String string = Arrays.toString(arrayList.stream().toArray(value -> new String[value]));
System.out.println(string);
}
}
5、收集方法collect
5.1、收集到list集合
public class Stream09 {
public static void main(String[] args) {
/**
* collect(Collector collect) 收集流中的数据,放在集合中
*/
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list,"刘十三-男-19","王莹莹-女-18","李二壮-男-18","大牛-女-19");
/**
* 收集list集合中所有男性
*/
List<String> sexList = list.stream().filter(s -> s.split("-")[1].equals("男"))
.collect(Collectors.toList());
System.out.println(sexList);
}
}
5.2、收集到Set集合
注意:收集到set中会去掉重复数据
public class Stream09 {
public static void main(String[] args) {
/**
* collect(Collector collect) 收集流中的数据,放在集合中
*/
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list,"刘十三-男-19","王莹莹-女-18","李二壮-男-18","大牛-女-19");
/**
* 收集list集合中所有男性
*/
Set<String> sexSet = list.stream().filter(s -> s.split("-")[1].equals("男"))
.collect(Collectors.toSet());
System.out.println(sexSet);
}
}
5.3、收集到Map集合
public class Stream09 {
public static void main(String[] args) {
/**
* collect(Collector collect) 收集流中的数据,放在集合中
*/
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list,"刘十三-男-19","王莹莹-女-18","李二壮-男-18","大牛-女-19");
/**
* 收集list集合中所有男性
* 收集到map集合
* 键为姓名,值为年龄
*/
Map<String, String> map = list.stream().filter(s -> s.split("-")[1].equals("男"))
.collect(Collectors.toMap(s -> s.split("-")[0], o -> o.split("-")[2]));
System.out.println(map);
}
}
6、Stream综合练习
public class StreamDemo01 {
public static void main(String[] args) {
/**
* 1、定义两个集合存储6名男演员,6名女演员,用逗号隔开,如:张三,23
* 2、男演员只要名字为3个字的前两人
* 3、女演员只要姓杨的,不要第一个
* 4、过滤后合并在一起
* 5、将过滤的演员封装成Actor对象
* 6、将所有演员对象保存在list集合中
* 备注:演员类 Actor,属性有name,age
*/
//定义两个Arrlist集合
ArrayList<String> arrayListMan = new ArrayList<>();
ArrayList<String> arrayListWoman = new ArrayList<>();
//存放集合
ArrayList<Actor> actors = new ArrayList<>();
//添加数据
Collections.addAll(arrayListMan,"张三,23","李四,24","王五,25","赵六哇,26","陈七公,27","宋八家,28");
Collections.addAll(arrayListWoman,"小红,23","杨吴,24","杨张,25","杨李,26","小林,27","小江,28");
Stream.concat(
arrayListMan.stream().filter(s -> s.split(",")[0].length() >= 3).limit(2),
arrayListWoman.stream().filter(s -> s.split(",")[0].startsWith("杨")).skip(1)
).forEach(s -> actors.add(new Actor(s.split(",")[0],s.split(",")[1])));
System.out.println(actors);
}
}