百知教育 - 孙帅 - 20_Lambda表达式与Stream编程
01_Java8中的接口
-
接口的新语法:
-
代码:
package day26; import java.util.Comparator; import java.util.List; import java.util.Arrays; import java.util.function.Consumer; public class TestInterface8{ public static void main(String[] args){ Animal a = Animal.creatDog(); Comparator<String> c1 = Comparator.naturalOrder(); Comparator<String> c2 = c1.reversed(); List<String> list = Arrays.asList("YKY","HHH","ZYK","LYH"); list.sort(c2); list.forEach(new Consumer<String>(){ public void accept(String s){ System.out.println(s); } }); Impl i = new Impl(); i.m2(); } } interface Animal{ void eat(); static Animal creatDog(){ class Dog implements Animal{ public void eat(){ System.out.println("Dog eat"); } } return new Dog(); } static Animal creatCat(){ class Cat implements Animal{ public void eat(){ System.out.println("Cat eat"); } } return new Cat(); } } interface IA{ void m1(); default void m2(){ System.out.println("A m2"); } } interface IB{ default void m2(){ System.out.println("B m2"); } } class Impl implements IA , IB{ public void m1(){} public void m2(){ IA.super.m2(); IB.super.m2(); } }
-
运行结果:
02_Lambda表达式
-
用法:
用于 实现接口(接口中只能有一个方法),并创建对象,即是 匿名内部类的简化写法。 -
语法:
- (参数表)->{方法实现}
- (参数表)->表达式 <==> return 表达式;
- (参数表)->单句实现(return 语句不能视为单句实现)
-
代码1:
package day26; import java.util.List; import java.util.ArrayList; public class TestLambda1{ public static void main(String[] args){ List<Student> list = new ArrayList<>(); list.add(new Student("YKY" , 20, 100.0, "C1")); list.add(new Student("HHH" , 20, 98.0, "C2")); list.add(new Student("LYH" ,19, 99.5, "C1")); list.add(new Student("YXM" , 18, 88.0, "C2")); list.add(new Student("ZYK" , 18, 87.0, "C1")); list.add(new Student("WKY" , 17, 59.0, "C2")); List<Student> result = filterStudents(list , new Filter(){ public boolean test(Student s){ if(s.getClassNumber().equals("C1")) return true; else return false; } }); Filter f1 = new Filter(){ public boolean test(Student s){return s.getAge()<19;} }; Filter f2 = (Student s)->{return s.getAge()<19;}; Filter f3 = (Student s)->s.getAge()<19; Filter f4 = (s)->s.getAge()<19; List<Student> result1 = filterStudents(list , (s)->s.getAge()<19); List<Student> result2 = filterStudents(list , (s)->s.getAge()<19 && s.getClassNumber().equals("C1")); for(Student s : result1){ System.out.println(s); } System.out.println(); for(Student s : result2){ System.out.println(s); } } static List<Student> filterStudents(List<Student> list , Filter filter){ List<Student> result = new ArrayList<>(); for(Student s : list){ if(filter.test(s)) result.add(s); } return result; } } class Student{ private String name; private int age; private double score; private String classNumber; public Student(String name , int age , double score , String classNumber){ super(); this.name = name; this.age = age; this.score = score; this.classNumber = classNumber; } @Override public String toString(){ return "Student [name="+name+", age="+age+", score="+score +", classNumber="+classNumber+"]"; } public String getName(){ return name; } public int getAge(){ return age; } public double getScore(){ return score; } public String getClassNumber(){ return classNumber; } } interface Filter{ boolean test(Student s); }
-
运行结果:
-
代码2:
package day26; import java.util.List; import java.util.Arrays; import java.util.function.Consumer; import java.util.Comparator; public class TestLambda{ public static void main(String[] args) throws Exception{ Thread t = new Thread(new Runnable(){ public void run(){ System.out.println("hello"); } }); Thread t2 = new Thread(()->System.out.println("hello")); t.start(); t.join(); System.out.println("\n"+"遍历集合:"); List<String> list = Arrays.asList("A" , "BB" , "CCC"); /* list.forEach(new Consumer<String>(){ public void accept(String s){ System.out.println(s); } }); */ list.forEach((s)->System.out.println(s)); System.out.println("\n"+"按从长到短排序为:"); list.sort(new Comparator<String>(){ public int compare(String s1 , String s2){ return s2.length()-s1.length(); } }); list.sort((s1,s2)->s2.length()-s1.length()); for(String s : list){ System.out.println(s); } } }
-
运行结果:
03_Lambda与泛型
-
函数式接口:
只有 一个 抽象方法的接口 -
@FunctionalInterface:
标注函数式接口 -
函数描述符形如:
- ()->void
- (T)->T/T->T
- (T,T)->int
-
代码:
package day26; import java.util.List; import java.util.Arrays; import java.util.ArrayList; public class TestLambda1{ public static void main(String[] args){ List<Student> list = new ArrayList<>(); list.add(new Student("YKY" , 20, 100.0, "C1")); list.add(new Student("HHH" , 20, 98.0, "C2")); list.add(new Student("LYH" ,19, 99.5, "C1")); list.add(new Student("YXM" , 18, 88.0, "C2")); list.add(new Student("ZYK" , 18, 87.0, "C1")); list.add(new Student("WKY" , 17, 59.0, "C2")); List<Student> result = filterObjects(list , new Filter<Student>(){ public boolean test(Student s){ if(s.getClassNumber().equals("C1")) return true; else return false; } }); //T->boolean Filter<Student> f1 = new Filter<Student>(){ public boolean test(Student s){return s.getAge()<19;} }; Filter<Student> f2 = (Student s)->{return s.getAge()<19;}; Filter<Student> f3 = (Student s)->s.getAge()<19; Filter<Student> f4 = (s)->s.getAge()<19; List<Student> result1 = filterObjects(list , (s)->s.getAge()<19); List<Student> result2 = filterObjects(list , (s)->s.getAge()<19 && s.getClassNumber().equals("C1")); for(Student s : result1){ System.out.println(s); } System.out.println(); for(Student s : result2){ System.out.println(s); } System.out.println("\n"+"筛选偶数:"); List<Integer> ls = Arrays.asList(1,3,5,2,6); List<Integer> rs = filterObjects(ls , (i)->(int)i%2==0); for(Integer i : rs){ System.out.println(i); } } static <T> List<T> filterObjects(List<T> list , Filter<T> filter){ List<T> result = new ArrayList<>(); for(T s : list){ if(filter.test(s)) result.add(s); } return result; } } class Student{ private String name; private int age; private double score; private String classNumber; public Student(String name , int age , double score , String classNumber){ super(); this.name = name; this.age = age; this.score = score; this.classNumber = classNumber; } @Override public String toString(){ return "Student [name="+name+", age="+age+", score="+score +", classNumber="+classNumber+"]"; } public String getName(){ return name; } public int getAge(){ return age; } public double getScore(){ return score; } public String getClassNumber(){ return classNumber; } } @FunctionalInterface interface Filter<T>{ boolean test(T s); }
-
运行结果: