lambda表达式
语法格式一:无参,无返回值
@Test
public void test1(){
Runnable r1 = ()-> System.out.println("hello lambda");
r1.run();
}
语法格式二:有多个参数,有返回值,并且lambad体中有多条语句
@Test
public void test3(){
//里面的(Integer x,Integer y)要么写要么都不写,不写的话jvm会根据上下文推断出类型
Comparator<Integer> con = (x,y)->{
System.out.println("函数示接口");
return Integer.compare(x,y);
};
}
2.lamdba表达式需要函数式接口的支持,
函数式接口:接口中只有一个抽象方法的接口,称为函数式接口。@FunctionalInterface有这个修饰的只有有一个接口
语法三:自定义一个接口然后用lambda调用
@FunctionalInterface
public interface MyFun {
Integer getValue(Integer num);
}
---------------------
@Test
public void test4(){
Integer num = opreation(100, (x) -> x * x);
System.out.println(num);
}
public Integer opreation(Integer num, MyFun myFun){
return myFun.getValue(num);
}
练习题:
1.调用Collections.sort()方法,通过定制排序比较两个Employee(先按年龄比,年龄相同按姓名比)使用lambda作为参数传递。
2.(1)声明函数式接口,接口中声明抽象方法,public String getValue(String str);
(2)声明类TestLambda,类中编写方法使用接口作为参数,将一个字符串转成大写,并作为方法的返回值。
(3)再将一个字符串的第2个和第4个索引位置进行截取字符串。
3(1)声明一个带两个泛型的函数式接口,泛型类型为<T,R>T为参数,R为返回值。
(2)接口中声明对应抽象方法。
(3)在TestLambda类中声明方法,使用接口作为参数,计算两个long型参数的和。
(4)在计算两个long类型参数的乘积。
解答:
解答第一个:.调用Collections.sort()方法,通过定制排序比较两个Employee(先按年龄比,年龄相同按姓名比)使用lambda作为参数传递。
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Employee {
private int id;
private String name;
private int age;
private double salary;
}
------------
public class TestLambda2 {
List<Employee> emps = Arrays.asList(
new Employee(101,"张三",18,999.11),
new Employee(102,"李四",57,444),
new Employee(103,"王五",23,555.111),
new Employee(104,"赵六",44,777.22),
new Employee(105,"田七",67,443.22)
);
@Test
public void test1(){
Collections.sort(emps,(e1,e2)->{
if(e1.getAge()==e2.getAge()){
return e1.getName().compareTo(e2.getName());
}else{
return Integer.compare(e1.getAge(),e2.getAge());
}
});
emps.forEach(item-> System.out.println(item.getId()));
}
}
2.小写转大写
@FunctionalInterface
public interface Myfunction {
String getVaule(String str);
}
---------------------
@Test
public void test2(){
//去除空格
System.out.println(strHander("\t \t \t 你好", (x) -> x.trim()));
//小写转大写
System.out.println(strHander("abcdfe", (x) -> x.toUpperCase()));
//截取2--4之间的字符串
System.out.println(strHander("abcdddead", (t) -> t.substring(2, 4)));
}
public String strHander(String str, Myfunction myfunction){
return myfunction.getVaule(str);
}
3.解答:使用接口作为参数,计算两个long型参数的和,乘积
@FunctionalInterface
public interface Myfun<T,R> {
R getValue(T t1,T t2);
}
--------------
@Test
public void test3(){
opearte(100L,200L,(x,y)-> x + y);
opearte(2L,44L,(a,b)-> a* b);
}
public void opearte(Long l1,Long l2, Myfun<Long,Long> myfun){
System.out.println(myfun.getValue(l1,l2));
}
第二章节
java8内置的四个核心函数式接口
函数式接口 | 参数类型 | 返回值 | 用途:方法 |
---|---|---|---|
Consumer消费型接口 | T | void | void accept(T t); |
Supplier 供给型接口 | 无 | T | T get(); |
Function<T,R>函数型接口 | T | R | R apply(T t); |
Predicate 判断型接口 | T | boolean | boolean test(T t); |
//消费型,无返回值,有参数类型
public void consumer(double money,Consumer<Double> consumer){
consumer.accept(money);
}
@Test
public void test4(){
consumer(500, (x)-> System.out.println(x));
}
//供给型接口,有返回值,无参数
public List<Integer> supplier(int num, Supplier<Integer> sup){
List<Integer> list = new ArrayList<>();
for (int i = 0; i < num; i++) {
list.add(sup.get());
}
return list;
}
@Test
public void test5(){
Random random = new Random();
List<Integer> list = supplier(10,()->random.nextInt(10));
list.forEach(System.out::println);
}
//函数型接口,有参数有返回值
public String strHander1(String srt, Function<String,String> function){
return function.apply(srt);
}
//函数型有返回值,有参数
@Test
public void test6(){
System.out.println(strHander1("abcd ff", (x) -> x.trim()));
}
//断言型,有参数,返回值为boolean
public boolean pretest(String str, Predicate<String> predicate){
return predicate.test(str);
}
@Test
public void test7(){
boolean pretest = pretest("abc", (x) -> x.equals("bcd"));
System.out.println(pretest);
}