0
点赞
收藏
分享

微信扫一扫

lambda 笔记


 第一部分:

package com.azzan.lambda;

/**
* 简介:
* 1:lambda 介绍
* 2:lambda 语法
* 3:lambda 语法精简
* 4:lambda 方法引用
* 5:lambda 综合案例
*/
public class LambdaTest {
public static void main(String[] args) {

//lambda
//语法结构 () -> {};
//():方法列表
//{}:方法体
//->:lambda运算符 称为 goes to

//多参有返回值
/*
简化前:
LambdaOne lambdaOne = (int a, int b) -> {
return a+b;
};
参数列表中的参数类型如果是多个的话,它们的参数类型要么都去掉,要么都保留,不允许一个参数有参数类型,一个参数无参数类型
*/
//简化后:
LambdaOne lambdaOne = (a, b) -> {
return a+b;
};
int ret = lambdaOne.test(10, 20);
System.out.println(ret);
//语法简化:
//因为方法体中只有一句语句,所以可以将花括号去掉,同时如果有返回值的,那么也把return去掉
//简化前:LambdaOne lambda1 = (a,b) -> { return a+b;};
//简化后:
LambdaOne lambda1 = (a,b) -> a+b;
int test = lambda1.test(11, 22);
System.out.println(test);

//2:多参无返回值
LambdaTwo lambdaTwo = (a,b) -> System.out.println(a+b);
lambdaTwo.test(20,30);

//3:无参无返回值
LambdaThree lambdaThree = () -> System.out.println("Hello World");
lambdaThree.test();

//lambda方法的引用
//1:无参构造方法的引用
CreateStudent createStudent = Student::new;
createStudent.getStudent();
//2:有参构造方法的引用
CreatStudentInfo creatStudentInfo = Student::new;
creatStudentInfo.getStudent("Azzan", 18);
}
}

//创建学生调用无参构造方法
@FunctionalInterface
interface CreateStudent{

Student getStudent();
}
//创建学生调用有参构造方法
@FunctionalInterface
interface CreatStudentInfo{
Student getStudent(String name,int age);
}

//有多参有返回值
@FunctionalInterface
interface LambdaOne{
int test(int a, int b);
}
//有多参无返回值
@FunctionalInterface
interface LambdaTwo{
void test(int a, int b);
}

//无参无返回值
@FunctionalInterface
interface LambdaThree{
void test();
}

lambda综合案例: 

package com.azzan.lambdause;

import com.azzan.lambda.Student;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
* lambda综合案例
*/
public class LambdaUse {
public static void main(String[] args) {
//ArrayList<>
List<Student> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
InitStudent initStudent = Student::new;
Student student = initStudent.getStudent("xiaoming"+i,10+ i);
list.add(student);
}

System.out.println(list.toString());

System.out.println("----------------------------");

//排序
//降序
list.sort((o1, o2) -> o2.getAge()-o1.getAge());
System.out.println(list.toString());

test();
testTwo();
testThree();
createThread();
}

//通过lambda开辟线程
public static void createThread(){
Thread thread = new Thread(() -> {
for (int i = 0; i < 20; i++) {
System.out.println(i);
}
});
thread.run();
}

//通过lambda实现集合的遍历
public static void testThree(){
List<Integer> list = new ArrayList<Integer>();
Collections.addAll(list,1,2,3,4,5,6,7,8,9,0);
System.out.println(list.toString());//输出打印的元素

//lambda实现元素的遍历
list.forEach(ele -> System.out.println(ele));

//lambda实现的删除集合元素
list.removeIf(ele -> ele > 6);
System.out.println(list.toString());

}

public static void test(){
InitNum initNum = (a, b) -> a+b;
int numCount = initNum.numCount(10, 20);
System.out.println(numCount);
}

public static void testTwo(){
//引用方法
InitNum initNum = LambdaUse::getTotal;
int numCount = initNum.numCount(11, 20);
System.out.println(numCount);
}

//如果该方法为静态方法,static修饰,那么该方法隶属于当前类
//如果该方法为非静态,那么该方法隶属于当前对象
public static int getTotal(int a, int b){
return a+b;
}

}

@FunctionalInterface
interface InitStudent{
Student getStudent(String name,int age);
}
@FunctionalInterface
interface InitNum{
int numCount(int a,int b);
}



举报

相关推荐

0 条评论