1、Lambda表达式
Lambda是一个匿名函数,我们可以把Lambda表达式理解为是一段可传递的代码(将代码像数据一样进行传递),可以写出更简洁,更灵活的代码,作为一种更紧凑的代码风格,使Java语言表达能力得到了提升
表达式语法示例
import org.junit.jupiter.api.Test;
import java.util.Comparator;
import java.util.function.Consumer;
/**
* Lambda 表达式的基础语法:Java8中引入了一个新的操作符 “->” 该操作符成为箭头操作符或lambda操作符
* 箭头操作符将 Lambda 表达式拆分成两份
* 左侧:Lambda 表达式的参数列表
* 右侧:Lambda 表达式中所需执行的功能。即Lambda体
*
* 语法格式一:无参数。无返回值:() -> System.out.println("Hello Lambda!");
* 语法格式二:有一个参数,并且无返回值:(x) -> System.out.println(x);
* 语法格式三:若只有一个参数,小括号可以不写 x -> System.out.println(x);
* 语法格式四:有两个以上的参数,有返回值,并且 Lambda 体中有多条语句
* Comparator<Integer> com = (x,y) -> {
* System.out.println("函数时接口"+(x+y));
* return Integer.compare(1,10);
* };
* 语法格式五:若Lambda体中只有一条语句,return和大括号都可有省略不写
* 语法格式六:Lambda表达式的参数列表的数据类型可以省略不写,因为JVM编译器通过上下文推断出数据类型,即“类型推断”
*
*
* @author World
* @since 2021/7/20 17:40
*/
public class LambdaTest2 {
@Test
public void test01() {
// 匿名内部类
Runnable r = new Runnable() {
@Override
public void run() {
System.out.println("Hello world");
}
};
r.run();
System.out.println("----------------------");
// Lambda表达式
Runnable r1 = () -> System.out.println("Hello Lambda!");
r1.run();
}
@Test
public void tes02(){
Consumer<String> con = (x) -> System.out.println(x);
con.accept("hello world");
}
@Test
public void test03(){
Comparator<Integer> com = (x,y) -> {
System.out.println("函数时接口"+(x+y));
return Integer.compare(1,10);
};
}
@Test
public void test04(){
Comparator<Integer> com = (x,y) -> Integer.compare(x,y);
}
//运算
@Test
public void test06(){
Integer num = operation(10, (x) -> x * x);
Integer num1 = operation(10, (x) -> x + 100);
System.out.println(num);
System.out.println(num1);
}
public Integer operation(Integer num,MyFun myFun){
return myFun.getValue(num);
}
}
接口:
@FunctionalInterface
public interface MyFun {
public Integer getValue(Integer num);
}
public interface MyPredicate<T> {
public boolean test(T t);
}
练习:
实体类(Employee):
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class Employee {
private int id;
private String name;
private int age;
private double salary;
接口:
public interface MyFunction {
public String getValue(String str);
}
public interface MyFunction2<T,R> {
public R getValue(T t1,T t2);
}
代码:
import cn.jsl.test.Employee;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* @author World
* @since 2021/7/20 20:52
*/
public class LambdaTest {
List<Employee> employees = Arrays.asList(
new Employee(1,"张三",18,3333.33),
new Employee(2,"李四",35,4444.44),
new Employee(3,"王五",37,5555.55)
);
@Test
public void test01() {
Collections.sort(employees,(e1,e2) -> {
if(e1.getAge() == e2.getAge()) {
return e1.getName().compareTo(e2.getName());
}else{
return Integer.compare(e1.getAge(),e2.getAge());
}
});
for (Employee employee : employees) {
System.out.println(employee);
}
}
@Test
public void test02() {
String trimStr = strHandler("\t\t\t Hello World ", (str) -> str.trim());
System.out.println(trimStr);
String abc = strHandler("abc", (str) -> str.toUpperCase());
System.out.println(abc);
String string = strHandler("哈哈哈嘿嘿嘿呵呵呵", (str) -> str.substring(3, 6));
System.out.println(string);
}
/**
* 处理字符串
* @return 处理字符串
*/
public String strHandler(String str, MyFunction myFunction) {
return myFunction.getValue(str);
}
@Test
public void test03() {
op(100L,200L, (x,y) -> x + y);
op(10L,10L, (x,y) -> x * y);
}
/**
* 处理两个Long型数据
*/
public void op(Long l1,Long l2,MyFunction2<Long,Long> myFunction) {
System.out.println(myFunction.getValue(l1,l2));
}
}