本章概述:本章主要讲了Lambda表达式在Java中的使用,Lambda表达式的介绍,Lambda表达式的使用,函数式接口,Lambda表达式的原理。
目录
本章概述:
一、Lambda
1、LambdaTest
2、StudentDao
3、Student
4、Teacher
5、TeacherDao
二、why1
1、Student
2、Test
三、why2
1、AgeFilter
2、ScoreFilter
3、StudentFilter
4、Test
四、why3
1、Test
五、why4
1、Test
六、LambdaDemo
七、LamdbaInterface
本章概述: 一、Lambda 1、LambdaTest
package com.class4.lambda;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import java.util.concurrent.Callable;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* @Auther: Yu Panpan
* @Date: 2022/5/3 - 05 - 03 - 16:08
* @Description: com.class4.lambda
* @version: 1.0
*/
public class LambdaTest {
public static void main(String[] args) throws Exception {
Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println("running1!");
}
};
runnable.run();
Runnable runnable2 = ()-> System.out.println("running2!");
Runnable runnable3 = ()->{
System.out.println("running3!");
};
runnable2.run();
runnable3.run();
Callable callable = new Callable() {
@Override
public String call() throws Exception {
return "jason1";
}
};
System.out.println(callable.call());
Callable callable1 = ()->{return "jason2";};
Callable callable2 = ()->"jason3";
System.out.println(callable1.call());
System.out.println(callable2.call());
StudentDao dao = new StudentDao() {
@Override
public void insert(Student student) {
System.out.println("插入1个学生记录:" + student);
}
};
dao.insert(new Student());
StudentDao dao1 = (student)-> System.out.println("插入:" + student);
StudentDao dao2 = (Student student)-> System.out.println("插入:" + student);
StudentDao dao3 = (student)->{
System.out.println("插入:" +student);
};
StudentDao dao4 = (Student student)-> {System.out.println("插入:" +student);};
StudentDao dao5 = student-> System.out.println("插入:" + student);
StudentDao dao6 = student-> {System.out.println("插入:" + student);};
dao1.insert(new Student());
dao2.insert(new Student());
dao3.insert(new Student());
dao4.insert(new Student());
dao5.insert(new Student());
dao6.insert(new Student());
TeacherDao teacherDao = new TeacherDao() {
@Override
public int get(Teacher teacher) {
return 1;
}
};
System.out.println(teacherDao.get(new Teacher()));
TeacherDao teacherDao1 =(teacher)->{return 2;};
TeacherDao teacherDao2 = (teacher)->3;
TeacherDao teacherDao3 = teacher->{return 4;};
TeacherDao teacherDao4 = teacher->5;
System.out.println(teacherDao1.get(new Teacher()));
System.out.println(teacherDao2.get(new Teacher()));
System.out.println(teacherDao3.get(new Teacher()));
System.out.println(teacherDao4.get(new Teacher()));
/*
* 在JDK中,提供了一些代表输入和输出的接口,他们本身是没有什么意义的,
* 只是为了方便的使用lambda表达式来编写代码,从而获取对应的执行结果。
*
*/
// Supplier supplier = new Supplier() {
// @Override
// public Integer get() {
// return 1;
// }
// };
Supplier supplier = ()->1;
System.out.println(supplier.get());
// Consumer consumer = new Consumer() {
// @Override
// public void accept(String s) {
// System.out.println(s);
// }
// };
Consumer consumer = (s)-> System.out.println(s);
consumer.accept("jason1");
// Function function = new Function() {
// @Override
// public String apply(Integer integer) {
// return integer + "jason2";
// }
// };
Function function = (i)->i+"jason3";
Function function1 = (Integer i)->{return i+"jason4";};
System.out.println(function.apply(200));
System.out.println(function1.apply(300));
}
}
2、StudentDao
package com.class4.lambda;
/**
* @Auther: Yu Panpan
* @Date: 2022/5/3 - 05 - 03 - 16:18
* @Description: com.class4.lambda
* @version: 1.0
*/
@FunctionalInterface
public interface StudentDao {
public void insert(Student student);
}
3、Student
package com.class4.lambda;
/**
* @Auther: Yu Panpan
* @Date: 2022/5/3 - 05 - 03 - 16:19
* @Description: com.class4.lambda
* @version: 1.0
*/
public class Student {
}
4、Teacher
package com.class4.lambda;
/**
* @Auther: Yu Panpan
* @Date: 2022/5/3 - 05 - 03 - 16:28
* @Description: com.class4.lambda
* @version: 1.0
*/
public class Teacher {
}
5、TeacherDao
package com.class4.lambda;
/**
* @Auther: Yu Panpan
* @Date: 2022/5/3 - 05 - 03 - 16:28
* @Description: com.class4.lambda
* @version: 1.0
*/
@FunctionalInterface
public interface TeacherDao {
public int get(Teacher teacher);
}
二、why1
1、Student
package com.class4.why1;
/**
* @Auther: Yu Panpan
* @Date: 2022/5/3 - 05 - 03 - 15:29
* @Description: com.class4.why1
* @version: 1.0
*/
public class Student {
private String name;
private int age;
private int score;
public Student() {
}
public Student(String name, int age, int score) {
this.name = name;
this.age = age;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", score=" + score +
'}';
}
}
2、Test
package com.class4.why1;
import java.util.ArrayList;
/**
* @Auther: Yu Panpan
* @Date: 2022/5/3 - 05 - 03 - 15:30
* @Description: com.class4.why1
* @version: 1.0
*/
public class Test {
public static void main(String[] args) {
ArrayList list = new ArrayList<>();
list.add(new Student("zhangfei",18,69));
list.add(new Student("guanyu",20,70));
list.add(new Student("zhaoyun",26,75));
list.add(new Student("liubei",30,85));
list.add(new Student("huangzhong",23,90));
//查询年龄大于25岁的学生信息
getByAge(list);
//查询分数大于80分的学生信息
System.out.println("******************************************");
getByScore(list);
}
public static void getByAge(ArrayList students){
ArrayList list = new ArrayList<>();
for(Student student : students){
if(student.getAge() > 25){
list.add(student);
}
}
for(Student student : list){
System.out.println(student);
}
}
public static void getByScore(ArrayList students){
ArrayList list = new ArrayList<>();
for(Student student : students){
if(student.getScore() > 80){
list.add(student);
}
}
for(Student student : list){
System.out.println(student);
}
}
}
三、why2
1、AgeFilter
package com.class4.why2;
import com.class4.why1.Student;
/**
* @Auther: Yu Panpan
* @Date: 2022/5/3 - 05 - 03 - 15:38
* @Description: com.class4.why2
* @version: 1.0
*/
public class AgeFilter implements StudentFilter {
@Override
public boolean compare(Student student) {
return student.getAge() > 25;
}
}
2、ScoreFilter
package com.class4.why2;
import com.class4.why1.Student;
/**
* @Auther: Yu Panpan
* @Date: 2022/5/3 - 05 - 03 - 15:39
* @Description: com.class4.why2
* @version: 1.0
*/
public class ScoreFilter implements StudentFilter {
@Override
public boolean compare(Student student) {
return student.getScore() > 80;
}
}
3、StudentFilter
package com.class4.why2;
import com.class4.why1.Student;
/**
* @Auther: Yu Panpan
* @Date: 2022/5/3 - 05 - 03 - 15:37
* @Description: com.class4.why2
* @version: 1.0
*/
public interface StudentFilter {
public boolean compare(Student student);
}
4、Test
package com.class4.why2;
import com.class4.why1.Student;
import java.util.ArrayList;
/**
* @Auther: Yu Panpan
* @Date: 2022/5/3 - 05 - 03 - 15:39
* @Description: com.class4.why2
* @version: 1.0
*/
public class Test {
public static void main(String[] args) {
ArrayList list = new ArrayList<>();
list.add(new Student("zhangfei",18,69));
list.add(new Student("guanyu",20,70));
list.add(new Student("zhaoyun",26,75));
list.add(new Student("liubei",30,85));
list.add(new Student("huangzhong",23,90));
//查询年龄大于25岁的学生信息
getByFilter(list,new AgeFilter());
//查询分数大于80分的学生信息
System.out.println("**************************************************");
getByFilter(list,new ScoreFilter());
}
public static void getByFilter(ArrayList students, StudentFilter filter){
ArrayList list = new ArrayList<>();
for(Student student : students){
if(filter.compare(student)){
list.add(student);
}
}
for(Student student : list){
System.out.println(student);
}
}
}
四、why3
1、Test
package com.class4.why3;
import com.class4.why1.Student;
import com.class4.why2.AgeFilter;
import com.class4.why2.StudentFilter;
import java.util.ArrayList;
/**
* @Auther: Yu Panpan
* @Date: 2022/5/3 - 05 - 03 - 15:44
* @Description: com.class4.why3
* @version: 1.0
*/
public class Test {
public static void main(String[] args) {
ArrayList list = new ArrayList<>();
list.add(new Student("zhangfei",18,69));
list.add(new Student("guanyu",20,70));
list.add(new Student("zhaoyun",26,75));
list.add(new Student("liubei",30,85));
list.add(new Student("huangzhong",23,90));
//查询年龄大于25岁的学生信息
getByFilter(list, new StudentFilter() {
@Override
public boolean compare(Student student) {
return student.getAge() > 25;
}
});
//查询分数大于80分的学生信息
System.out.println("******************************************************************");
getByFilter(list, new StudentFilter() {
@Override
public boolean compare(Student student) {
return student.getScore() > 80;
}
});
//查询姓名长度大于6的学生信息
System.out.println("******************************************************************");
getByFilter(list, new StudentFilter() {
@Override
public boolean compare(Student student) {
return student.getName().length() > 6;
}
});
}
public static void getByFilter(ArrayList students, StudentFilter filter){
ArrayList list = new ArrayList<>();
for(Student student : students){
if(filter.compare(student)){
list.add(student);
}
}
printStudent(list);
}
public static void printStudent(ArrayList students){
for(Student student : students) {
System.out.println(student);
}
}
}
五、why4
1、Test
package com.class4.why4;
import com.class4.why1.Student;
import com.class4.why2.StudentFilter;
import java.util.ArrayList;
/**
* @Auther: Yu Panpan
* @Date: 2022/5/3 - 05 - 03 - 15:59
* @Description: com.class4.why4
* @version: 1.0
*/
public class Test {
public static void main(String[] args) {
ArrayList list = new ArrayList<>();
list.add(new Student("zhangfei",18,69));
list.add(new Student("guanyu",20,70));
list.add(new Student("zhaoyun",26,75));
list.add(new Student("liubei",30,85));
list.add(new Student("huangzhong",23,90));
//查询年龄大于25岁的学生信息
getByFilter(list,(student)->student.getAge()>25);
//查询分数大于80分的学生信息
System.out.println("**************************************************************");
getByFilter(list,(student)->student.getScore() > 80);
//查询姓名长度大于6的学生信息
System.out.println("**************************************************************");
getByFilter(list,(student)->student.getName().length()>6);
}
public static void getByFilter(ArrayList students, StudentFilter filter){
ArrayList list = new ArrayList<>();
for(Student student : students){
if(filter.compare(student)){
list.add(student);
}
}
printStudent(list);
}
public static void printStudent(ArrayList students){
for(Student student : students) {
System.out.println(student);
}
}
}
六、LambdaDemo
package com.class4;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* @Auther: Yu Panpan
* @Date: 2022/5/3 - 05 - 03 - 15:21
* @Description: com.class4
* @version: 1.0
*/
public class LambdaDemo {
public static void main(String[] args) {
// Thread thread =new Thread(new Runnable() {
// @Override
// public void run() {
// System.out.println("running!");
// }
// });
// thread.start();
// new Thread(()-> System.out.println("running!")).start();
List list = Arrays.asList("java", "go", "python", "scala", "javascript");
// Collections.sort(list, new Comparator() {
// @Override
// public int compare(String o1, String o2) {
// return o1.length() - o2.length();
// }
// });
// for(String str : list){
// System.out.println(str);
// }
Collections.sort(list,(a,b)->a.length()-b.length());
list.forEach(System.out::println);
}
}
七、LamdbaInterface
package com.class4;
/**
* @Auther: Yu Panpan
* @Date: 2022/5/3 - 05 - 03 - 16:06
* @Description: com.class4
* @version: 1.0
*/
@FunctionalInterface
public interface LamdbaInterface {
// public void add(int a,int b);
public void sub(int a,int b);
}
八、LambdaTest
package com.class4.lambda;
import com.class4.LamdbaInterface;
import java.util.concurrent.Callable;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* @Auther: Yu Panpan
* @Date: 2022/5/3 - 05 - 03 - 16:08
* @Description: com.class4.lambda
* @version: 1.0
*/
public class LambdaTest {
public static void main(String[] args) throws Exception {
// Runnable runnable = new Runnable() {
// @Override
// public void run() {
// System.out.println("running1!");
// }
// };
// runnable.run();
//
// Runnable runnable2 = ()-> System.out.println("running2!");
// Runnable runnable3 = ()->{
// System.out.println("running3!");
// };
// runnable2.run();
// runnable3.run();
//
// Callable callable = new Callable() {
// @Override
// public String call() throws Exception {
// return "jason1";
// }
// };
// System.out.println(callable.call());
//
// Callable callable1 = ()->{return "jason2";};
// Callable callable2 = ()->"jason3";
// System.out.println(callable1.call());
// System.out.println(callable2.call());
//
// StudentDao dao = new StudentDao() {
// @Override
// public void insert(Student student) {
// System.out.println("插入1个学生记录:" + student);
// }
// };
// dao.insert(new Student());
// StudentDao dao1 = (student)-> System.out.println("插入:" + student);
// StudentDao dao2 = (Student student)-> System.out.println("插入:" + student);
// StudentDao dao3 = (student)->{
// System.out.println("插入:" +student);
// };
// StudentDao dao4 = (Student student)-> {System.out.println("插入:" +student);};
// StudentDao dao5 = student-> System.out.println("插入:" + student);
// StudentDao dao6 = student-> {System.out.println("插入:" + student);};
//
// dao1.insert(new Student());
// dao2.insert(new Student());
// dao3.insert(new Student());
// dao4.insert(new Student());
// dao5.insert(new Student());
// dao6.insert(new Student());
//
// TeacherDao teacherDao = new TeacherDao() {
// @Override
// public int get(Teacher teacher) {
// return 1;
// }
// };
// System.out.println(teacherDao.get(new Teacher()));
//
// TeacherDao teacherDao1 =(teacher)->{return 2;};
// TeacherDao teacherDao2 = (teacher)->3;
// TeacherDao teacherDao3 = teacher->{return 4;};
// TeacherDao teacherDao4 = teacher->5;
// System.out.println(teacherDao1.get(new Teacher()));
// System.out.println(teacherDao2.get(new Teacher()));
// System.out.println(teacherDao3.get(new Teacher()));
// System.out.println(teacherDao4.get(new Teacher()));
/*
* 在JDK中,提供了一些代表输入和输出的接口,他们本身是没有什么意义的,
* 只是为了方便的使用lambda表达式来编写代码,从而获取对应的执行结果。
*
*/
// Supplier supplier = new Supplier() {
// @Override
// public Integer get() {
// return 1;
// }
// };
// Supplier supplier = ()->1;
// System.out.println(supplier.get());
// Consumer consumer = new Consumer() {
// @Override
// public void accept(String s) {
// System.out.println(s);
// }
// };
// Consumer consumer = (s)-> System.out.println(s);
// consumer.accept("jason1");
// Function function = new Function() {
// @Override
// public String apply(Integer integer) {
// return integer + "jason2";
// }
// };
// Function function = (i)->i+"jason3";
// Function function1 = (Integer i)->{return i+"jason4";};
// System.out.println(function.apply(200));
// System.out.println(function1.apply(300));
// BiFunction bf = (a,b)->a.length()+b.length();
// System.out.println(bf.apply("老于", "很帅"));
//
// Runnable runnable1 = ()-> System.out.println(get());
// Runnable runnable2 = ()->{
// String str = find();
// System.out.println(str);
// };
// Runnable runnable3 = ()->exec();
// Runnable runnable4 = ()->1;
// Runnable runnable5 = ()->"ok";
// Runnable runnable6 = ()->get();
// Runnable runnable7 = ()->true?1:2;
// runnable1.run();
// runnable2.run();
// runnable3.run();
// LambdaInterface li1 = new LambdaInterface() {
// @Override
// public int get(int num) {
// return num;
// }
// };
// System.out.println(li1.get(200));
// LambdaInterface li1 = (num)-> get(num);
// LambdaInterface li2 = (num)-> System.out.println(num);
// LambdaInterface li3 = (num)-> {return get(num);};
LambdaInterface li4 = (num)-> {return num;};
LambdaInterface li5 = (num)-> num;
LambdaInterface li6 = num-> num;
LambdaInterface li7 = num-> {return num;};
System.out.println(li4.get(300));
Function function = (num)->num;
System.out.println(function.apply(100));
Consumer consumer1 = (num)->System.out.println(num);
consumer1.accept(1000);
Consumer consumer2 = (num)->getNum(num);
consumer2.accept(2000);
Function f1 = (str)->str.toUpperCase();
Function f2 = (str)->toUpperCase(str);
System.out.println(f1.apply("abcdefg"));
System.out.println(f2.apply("abcdefgefg"));
BiFunction bf = (a,b)->a.length()+b.length();
System.out.println(bf.apply("老于", "很帅"));
BiFunction bf1 = (a,b)->getStrLength(a,b);
System.out.println(bf1.apply("老于", "很帅"));
}
public static void getNum(int num){
System.out.println(num);
}
public static String toUpperCase(String str){
return str.toUpperCase();
}
public static Integer getStrLength(String str1,String str2){
return str1.length() + str2.length();
}
// public static int get(){
// return 1;
// }
//
// public static String find(){
// return "find";
// }
//
// public static void exec(){
// find();
// }
}
九、LambdaInterface
package com.class4.lambda;
/**
* @Auther: Yu Panpan
* @Date: 2022/5/4 - 05 - 04 - 8:22
* @Description: com.class4.lambda
* @version: 1.0
*/
@FunctionalInterface
public interface LambdaInterface {
int get(int num);
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)