1、lambda表达式的书写
(1)lambda表达式:在java中Lambda表达式是对象,他们必须依赖于一类特别的对象类型函数式接口
@FunctionalInterface interface MyInterface1{ void myMethod1(); } @FunctionalInterface interface MyInterface2{ void myMethod2(); } public class Test1 { public static void main(String[] args) { MyInterface1 interface1=()->{}; System.out.println(interface1.getClass().getInterfaces()[0]); MyInterface2 interface2=()->{}; System.out.println(interface2.getClass().getInterfaces()[0]); } }
表达式的书写一定要根据上下文信息(两个函数式接口):
例如:
interface MyInterface2{ void myMethod2(); }
接口是无参数的,那么lambda表达式的小括号代表参数内容(这里为空),{ },代表的是接口中的内容(这里面的函数式接口不接收参数,不返回值,():方法的参数,{}:方法的实现),函数式接口是直接找的抽象方法。
lambda操作符将lambda表达式分为两部分,左侧是lambda表达式的参数列表,右侧是lambda表达式需要执行的功能
(2)基本语法
- 无参数,无返回值:
Runnable r1 = () -> System.out.println("Hello Lambda!");
- 有一个参数,并且无返回值,若只有一个参数,小括号可以省略不写:
Consumer<String> con = x -> System.out.println(x);
- 有两个以上的参数,有返回值,并且 Lambda 体中有多条语句
Comparator<Integer> com = (x, y) -> { System.out.println("函数式接口"); return Integer.compare(x, y); };
若 Lambda 体中只有一条语句, return 和 大括号都可以省略不写
Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
Lambda 表达式的参数列表的数据类型可以省略不写,因为JVM编译器通过上下文推断出,数据类型,即“类型推断”
2、Runable接口
(1)它是一个函数式接口,因为该接口只有一个抽象方法:
接口中只有一个抽象方法的接口,称为函数式接口。 可以使用注解 @FunctionalInterface 修饰可以检查是否是函数式接口
@FunctionalInterface public interface Runnable { /** * When an object implementing interface <code>Runnable</code> is used * to create a thread, starting the thread causes the object's * <code>run</code> method to be called in that separately executing * thread. * <p> * The general contract of the method <code>run</code> is that it may * take any action whatsoever. * * @see java.lang.Thread#run() */ public abstract void run(); }
(2)创建一个线程:
普通方式:
new Thread(new Runnable() { @Override public void run() { } }).start();
lambda表达式:
new Thread(()->System.out.println("ni hao")).start();
3、应用
(1) 小写转换为大写:
public static void main(String[] args) { List<String> list= Arrays.asList("jia","you"); list.forEach(item->System.out.println(item.toUpperCase())); }
JIA YOU
(2)小写转大写后,再存储到一个新的集合中:
public static void main(String[] args) { List<String> list= Arrays.asList("jia","you"); List<String> list1=new ArrayList<>(); list.forEach(item->list1.add(item.toUpperCase())); list1.forEach(item->System.out.println(item)); }
转化为流后处理:
public static void main(String[] args) { List<String> list= Arrays.asList("jia","you"); list.stream().map(item->item.toUpperCase()).forEach(item->System.out.println(item)); }
方法的引用方式实现:
public static void main(String[] args) { List<String> list= Arrays.asList("jia","you"); list.stream().map(String::toUpperCase).forEach(item->System.out.println(item)); }
(3)比较器的运用
List<Employee> emps = Arrays.asList( new Employee(101, "张三", 18, 9999.99), new Employee(102, "李四", 59, 6666.66), new Employee(103, "王五", 28, 3333.33), new Employee(104, "赵六", 8, 7777.77), new Employee(105, "田七", 38, 5555.55) ); @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()); } }); for (Employee emp : emps) { System.out.println(emp); } }