lambda
表达式学习,抛开语法从匿名内部类学习语法特性:
共6种场景
1接收一个参数,没有返回值;
2接收两个参数,返回一个参数;
3无参数,无返回;
4构造方法引用;
5静态方法应用;
6实例(对象)方法引用。
定义一个接口(有且只有一个抽象方法)
public interface MyInterface { void print(String str); }
测试类
public class Test { public static void main(String[] args) { MyInterface m1 = new MyInterface() { @Override public void print(String str) { System.out.println(str); } }; //常规写法 MyInterface m2 = (String str) -> { System.out.println(str); }; //简写 <strong>MyInterface m3 = str -> System.out.println(str);</strong> m3.print("hello lambda.."); } }
定义一个接口
public interface MyInterface { int sum(int num1, int num2); }
测试类
public class Test { public static void main(String[] args) { /** * 匿名内部类方式 */ MyInterface m1 = new MyInterface() { @Override public int sum(int num1, int num2) { return num1 + num2; } }; /** * lambda常规写法 */ MyInterface m2 = (int num1, int num2) -> { return num1 + num2; }; /** * lambda简写 * 1 不写入参格式,可以自动推断 * 2 如果方法中语句只有一条可以省略{};如果是return返回数据,可以省略return */ <strong>MyInterface m3 = (num1, num2) -> num1 + num2;</strong> System.out.println(m3.sum(123, 456)); } }
定义一个接口
public interface MyInterface { void test(); }
测试类
public class Test { public static void main(String[] args) { MyInterface m1 = new MyInterface() { @Override public void test() { System.out.println(111); } }; //lambda表达式常规 MyInterface m2 = () -> System.out.println(111); //简写 MyInterface m3 = () -> { }; /** * 与匿名内部类一致,局部变量被final修饰 * age前有隐藏的final */ int age = 19; <strong>MyInterface m4 = () -> System.out.println(age);</strong> m4.test(); } }
创建实体对象
public class Person { private String firstName; private String lastName; public Person(String firstName, String lastName) { super(); this.firstName = firstName; this.lastName = lastName; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } @Override public String toString() { return "Person{" + "firstName='" + firstName + '/'' + ", lastName='" + lastName + '/'' + '}'; } }
创建工厂
public interface PersonFactory { Person createPerson(String firstName, String lastName); }
测试类
public class Test { public static void main(String[] args) { /* 通过工厂获取Person对象 */ PersonFactory pf = new PersonFactory() { @Override public Person createPerson(String firstName, String lastName) { return new Person(firstName, lastName); } }; //lambda常规写法 PersonFactory pf1 = (firstName, lastName) -> new Person(firstName, lastName); /* lambda常规写法,构造方法引用;简单写法 方法参数要一致 */ <strong>PersonFactory pf2 = Person::new;</strong> Person p = pf2.createPerson("learn", "java"); System.out.println(p); } }
定义接口(字符串转数字)
public interface ParseInterface { int parse(String str); }
测试类
public class Test { public static void main(String[] args) { ParseInterface p1 = new ParseInterface() { @Override public int parse(String str) { return Integer.parseInt(str); } }; //lambda常规写法 ParseInterface p2 = str -> Integer.parseInt(str); //静态方法引用简写(参数原封不动传给方法,类似构造方法引用) <strong>ParseInterface p3 = Integer::parseInt;</strong> System.out.println(p3.parse("123")); } }
需求:判断字符串后缀包含endWith
public class Test { public static void main(String[] args) { /* 字符串endWith,是否存在 */ //被判断的字符串对象 String str = "learn"; Function<String, Boolean> fun1 = new Function<String, Boolean>() { @Override public Boolean apply(String suffix) { return str.endsWith(suffix); } }; //lambda方法 Function<String, Boolean> fun2 = suffix -> str.endsWith(suffix); //简写;实例(对象)方法引用 <strong>Function<String, Boolean> fun3 = str::endsWith;</strong> System.out.println(fun3.apply("rn2")); } }