目录
lambda 表达式方法引用
方法引用满足条件:如果lambda表达式方法体已经有其他方法实现那么则可以使用方法引用
mySum方法引用sum方法
public class Demo {
public static void main(String[] args) {
// int c = a + b;
int c = mySum(10,20);
System.out.println(c);
}
public static int mySum(int a, int b){
// return a + b;
return sum(a, b);
}
public static int sum(int a,int b){
return a + b;
}
}
输出
30
lambda 表达式静态方法引用
静态方法引用使用 类名::方法名
Person类
public class Person {
private String nickname;
private int gender;
public Person() {
}
public Person(String nickname, int gender) {
this.nickname = nickname;
this.gender = gender;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
public static int staticSum(int a,int b){
return a + b;
}
public int sum(int a , int b){
return a + b;
}
@Override
public String toString() {
return "Person{" +
"nickname='" + nickname + '\'' +
", gender=" + gender +
'}';
}
}
测试类
public class LambdaTest {
@Test
public void test01(){
int i = Person.staticSum(10, 20);
BiFunction<Integer, Integer, Integer> biFunction = Person::staticSum;
Integer sum = biFunction.apply(10, 20);
System.out.println(sum);
}
@Test
public void test02(){
BiFunction<Integer,Integer,Integer> biFunction = (x, y) -> x + y;
Integer sum = biFunction.apply(10, 20);
System.out.println(sum);
}
}
输出
30
lambda 表达式实例方法引用
实例方法引用使用 实例名::方法名
public class LambdaTest {
@Test
public void test03(){
Person person = new Person();
BiFunction<Integer, Integer, Integer> biFunction = person::sum;
Integer sum = biFunction.apply(10, 20);
System.out.println(sum);
}
}
输出
30
lambda 表达式无参构造方法引用
无参构造方法引用使用 类名::new
public class LambdaTest {
@Test
public void test04(){
Supplier<Person> supplier = Person::new;
Person person = supplier.get();
person.setNickname("张三");
person.setGender(1);
System.out.println(person);
}
@Test
public void test05(){
Supplier<Person> supplier = () -> new Person();
Person person = supplier.get();
System.out.println(person);
}
}
输出
Person{nickname='张三', gender=1}
lambda 表达式有参构造方法引用
有参构造方法引用使用 类名::new
public class LambdaTest {
@Test
public void test06(){
BiFunction<String, Integer, Person> biFunction = (nickname, gender) -> new Person(nickname, gender);
Person person = biFunction.apply("张三",1);
System.out.println(person);
}
@Test
public void test07(){
BiFunction<String,Integer,Person> biFunction = Person::new;
Person person = biFunction.apply("张三",1);
System.out.println(person);
}
}
输出
Person{nickname='张三', gender=1}
lambda 表达式特殊实例方法引用
Person 实现 Comparable接口重写compareTo方法比较两个对象是否相等(相等返回0)
Person类
public class Person implements Comparable{
@Override
public int compareTo(Object o) {
Person person = (Person) o;
int x = this.gender - person.getGender();
int y = this.nickname.hashCode() - person.getNickname().hashCode();
if (x != 0){
return x;
}else if (y != 0){
return y;
}else {
return 0;
}
}
}
普通比较
public class LambdaTest {
@Test
public void test08(){
Person person01 = new Person("张三", 1);
Person person02 = new Person("李四", 0);
if (person01.compareTo(person02) != 0){
System.out.println("不是同一个对象");
}else {
System.out.println("是同一个对象");
}
}
}
输出
不是同一个对象
lambda 表达式方法引用
@Test
public void test10(){
BiFunction<Person, Person,Integer> biFunction = (person01, person02) -> person01.compareTo(person02);
Person person01 = new Person("张三", 1);
Person person02 = new Person("李四", 0);
Integer num = biFunction.apply(person01, person02);
if (num != 0){
System.out.println("不是同一个对象");
}else {
System.out.println("是同一个对象");
}
}
@Test
public void test11(){
// 特殊实例方法引用 第一个参数方法的参数是第二个参数
BiFunction<Person, Person,Integer> biFunction = Person::compareTo;
Person person01 = new Person("张三", 1);
Person person02 = new Person("李四", 0);
Integer num = biFunction.apply(person01, person02);
if (num != 0){
System.out.println("不是同一个对象");
}else {
System.out.println("是同一个对象");
}
}
}
输出
不是同一个对象
lambda 表达式数组引用
@Test
public void test12(){
// String[] names = new String[10];
Function<Integer,String[]> function = (count) -> new String[count];
String[] names = function.apply(10);
System.out.println(names.length);
}
@Test
public void test13(){
Function<Integer,String[]> function = String[]::new;
String[] names = function.apply(10);
System.out.println(names.length);
}
}
输出
10