import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Co {
public static void main(String[] args) {
//创建男演员对象
Actor actor1=new Actor("张三二",23);
Actor actor2=new Actor("李四",24);
Actor actor3=new Actor("王五",25);
Actor actor4=new Actor("成三",28);
Actor actor5=new Actor("李大壮",29);
Actor actor6=new Actor("王狗圣",33);
//创建演员对象
Actor actor7=new Actor("杨花",23);
Actor actor8=new Actor("杨乐",24);
Actor actor9=new Actor("王华",25);
Actor actor10=new Actor("小妹",28);
Actor actor11=new Actor("杨女家",29);
Actor actor12=new Actor("杨花花",33);
ArrayList<Actor> listNAN=new ArrayList<>();
ArrayList<Actor> listNV=new ArrayList<>();
Collections.addAll(listNAN,actor1,actor2,actor3,actor4,actor5,actor6);
Collections.addAll(listNV,actor7,actor8,actor9,actor10,actor11,actor12);
//男演员只要名字三个字的前两人
listNAN.stream().filter(new Predicate<Actor>() {
@Override
public boolean test(Actor actor) {
return actor.getName().length() == 3;
}
}).limit(2);
//女演员只要姓杨的且不要第一个
listNV.stream().filter(new Predicate<Actor>() {
@Override
public boolean test(Actor actor) {
return "杨".equals(actor.getName().substring(0,1));
}
}).skip(1);
//合并两个流存入List集合中 用lambda简化
List<Actor> resultlist = Stream.concat(listNAN.stream().filter(s -> s.getName().length() == 3).limit(2),
listNV.stream().filter(s -> "杨".equals(s.getName().substring(0, 1))).skip(1))
.collect(Collectors.toList());
for (Actor s : resultlist) {
System.out.println(s);
}
//Actor{name = 张三二, age = 23}
//Actor{name = 李大壮, age = 29}
//Actor{name = 杨乐, age = 24}
//Actor{name = 杨女家, age = 29}
//Actor{name = 杨花花, age = 33}
}
}
三、方法引用
练习一:
java复制代码
import java.util.*;
public class Co {
public static void main(String[] args) {
//引用构造方法 类名::new
//引用指定类型数组构造方法 数据类型[]::new
ArrayList<String> list=new ArrayList<>();
Collections.addAll(list,"张三,23","李四,18","王五,39","陈六,34");
Actor[] arr = list.stream().map(Actor::new).toArray(Actor[]::new);
System.out.println(Arrays.toString(arr));
//[Actor{name = 张三, age = 23}, Actor{name = 李四, age = 18},
// Actor{name = 王五, age = 39}, Actor{name = 陈六, age = 34}]
}
}
java复制代码
public Actor() {
}
//引用构造方法 所需要增加的新构造方法
public Actor(String s) {
this.name=s.split(",")[0];
this.age=Integer.parseInt(s.split(",")[1]);
}
public Actor(String name, int age) {
this.name = name;
this.age = age;
}
练习二:
java复制代码
import java.util.*;
import java.util.function.Function;
public class Co {
public static void main(String[] args) {
Actor actor1=new Actor("张三",20);
Actor actor2=new Actor("王五",10);
Actor actor3=new Actor("李四",40);
Actor actor4=new Actor("陈六",22);
ArrayList<Actor> list=new ArrayList<>();
Collections.addAll(list,actor1,actor2,actor3,actor4);
//使用类名引用成员方法
//独有规则:
//1、需要有函数式接口
//2、被引用方法必须已经存在
//3、被引用方法的形参需要跟抽象方法的第二个形参到最后一个形参保持一致,返回值需保持一致
//4、被引用方法的功能需要满足当前需求
//抽象方法形参的详解:
//第一个参数:表示被引用方法的调用者,决定了可以引用哪些类中的方法,在Stream流中,第一个参数一般表示流中每一个数据,假设流中数据为String类型,那么使用这种方法进行方法引用,只能引用String类中的方法。
//第二个参数到最后一个参数:跟被引用方法的形参保持一致,如果没有第二个参数,说明被引用的方法需要是无参的成员方法。
String[] arr = list.stream().map(Actor::getName).toArray(String[]::new);
System.out.println(Arrays.toString(arr)); //[张三, 王五, 李四, 陈六]
}
}
练习三:
java复制代码
import java.util.*;
public class Co {
public static void main(String[] args) {
Actor actor1=new Actor("张三",20);
Actor actor2=new Actor("王五",10);
Actor actor3=new Actor("李四",40);
Actor actor4=new Actor("陈六",22);
ArrayList<Actor> list=new ArrayList<>();
Collections.addAll(list,actor1,actor2,actor3,actor4);
//使用类名引用成员方法
String[] arr = list.stream().map(Actor::toString).toArray(String[]::new);
System.out.println(Arrays.toString(arr));
//[张三-20, 王五-10, 李四-40, 陈六-22]
}
}