运用点:
流内数据类型转换(map)、filter、limit、skip、concat(让两个流合并)
题目:
![](https://i-blog.csdnimg.cn/direct/1c58a8563e3e47e3bfc4cbb6d22058f6.png)
操作1、2:
ArrayList<String> manList = new ArrayList<>();
ArrayList<String> womanList = new ArrayList<>();
Collections.addAll(manList,"张三,23","李四,24","王五,25","大的的,27","啊范文,38","大阿迪,22");
Collections.addAll(womanList,"杨丹,23","杨健康,24","发烧大户,25","鱼蛋,27","杨贵妃,38","对我的,22");
Stream<String> stream1 = manList.stream().filter(s -> s.split(",")[1].length() == 3)
.limit(2);
Stream<String> stream2 = womanList.stream().filter(s -> s.startsWith("杨")).skip(1);
操作3、4、5:
map完整版:
Stream.concat(stream1,stream2).map(new Function<String, Actor>() {
@Override
public Actor apply(String s) {
String name = s.split(",")[0];
int age = Integer.parseInt(s.split(",")[1]);
Actor actor = new Actor(age, name);
return actor;
}
});
map稍微省略版:
Stream.concat(stream1,stream2).map( s ->{
String name = s.split(",")[0];
int age = Integer.parseInt(s.split(",")[1]);
Actor actor = new Actor(age, name);
return actor;
});
lambda:
List<Actor> list = Stream.concat(stream1, stream2).map(s -> new Actor(Integer.parseInt(s.split(",")[1]), s.split(",")[0]))
.collect(Collectors.toList());
System.out.println(list);