栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java(13)lambda表达式之方法引用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java(13)lambda表达式之方法引用

01 lambda表达式方法引用
package bean;

public class Person implements Comparable {
    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 +
                '}';
    }

    @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;
        }
    }
}
package Day16;

public class Demo {
    public static void main(String[] args) {
        // int c = 10 + 20;
        int c = mySum(10, 20);
        System.out.println(c);
    }
//30
    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;
    }
}
02 lambda表达静态式方法引用
package Test;

import bean.Person;
import com.google.common.base.Supplier;
import org.junit.jupiter.api.Test;

import java.util.function.BiFunction;


// 静态方法引用 类名::方法名
 public class LambdaTest {
    @Test
    public void test01() {
        int i = Person.staticSum(10, 20);
        BiFunction biFunction = Person::staticSum;
        Integer sum = biFunction.apply(10, 20);
        System.out.println(sum);
    }
//30
    @Test
    public void test02() {
        BiFunction biFunction = (x, y) -> x + y;
        Integer sum = biFunction.apply(10, 20);
        System.out.println(sum);
    }
//30
}
03 lambda表达式实例方法引用
//实例方法引用 实例名::方法名
   @Test
    public void test03() {
        Person person = new Person();
        BiFunction biFunction = person::sum;
        Integer sum = biFunction.apply(10, 20);
        System.out.println(sum);
    }
    //30
04 lambda表达式无参构造方法引用
    @Override
    public String toString() {
        return "Person{" +
                "nickname='" + nickname + ''' +
                ", gender=" + gender +
                '}';
    }
//无参构造方法引用 类名::new


    @Test
    public void test04() {
        Supplier supplier = Person::new;
        Person person = supplier.get();
        person.setNickname("xm");
        person.setGender(1);
        System.out.println(person);
    }
//Person{nickname='null', gender=0}
    @Test
    public void test05() {
        Supplier supplier = () -> new Person();
        Person person = supplier.get();
        System.out.println(person);
    }
//Person{nickname='null', gender=0}
05 lambda表达式有参构造方方法引用
//有参构造方法引用 类名::new

    @Test
    public void test06() {
        BiFunction biFunction = (nickname, gender) -> new Person(nickname, gender);
        Person person = biFunction.apply("xm", 1);
        System.out.println(person);
    }
//Person{nickname='xm', gender=1}
    @Test
    public void test07() {
        BiFunction biFunction = Person::new;
        Person person = biFunction.apply("xm", 1);
        System.out.println(person);
    }
//Person{nickname='xm', gender=1}
06 lambda表达式特殊实例方法引用
    @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;
        }
    }
//特殊实例方法引用 第一个参数的方法的参数是第二参数

    @Test
    public void test08() {
        Person person01 = new Person("xm", 1);
        Person person02 = new Person("x", 0);
        if (person01.compareTo(person02) != 0) {
            System.out.println("不是同一个对象");
        } else {
            System.out.println("是同一个对象");
        }
    }
//不是同一个对象
    @Test
    public void test09() {
        BiPredicate biPredicate = (person01, person02) -> {
            if (person01.compareTo(person02) != 0) {
                return false;
            } else {
                return true;
            }
        };
        Person person01 = new Person("xm", 1);
        Person person02 = new Person("x", 0);
        boolean test = biPredicate.test(person01, person02);
        if (test) {
            System.out.println("不是同一个对象");
        } else {
            System.out.println("是同一个对象");
        }
    }
//是同一个对象
    @Test
    public void test10() {
        BiFunction biPredicate = (person01, person02) -> person01.compareTo(person02);
        Person person01 = new Person("xm", 1);
        Person person02 = new Person("x", 0);
        Integer num = biPredicate.apply(person01, person02);
        if (num != 0) {
            System.out.println("不是同一个对象");
        } else {
            System.out.println("是同一个对象");
        }
    }
//不是同一个对象
    @Test
    public void test11() {
        BiFunction biPredicate = Person::compareTo;
        Person person01 = new Person("xm", 1);
        Person person02 = new Person("x", 0);
        Integer num = biPredicate.apply(person01, person02);
        if (num != 0) {
            System.out.println("不是同一个对象");
        } else {
            System.out.println("是同一个对象");
        }
    }
//不是同一个对象
07 lambda表达式数组引用
//数组引用

    @Test
    public void test12() {
        // String[] names = new String[10];
        Function function = (count) -> new String[count];
        String[] names = function.apply(10);
        System.out.println(names.length);
    }
//10
    @Test
    public void test13() {
        // String[] names = new String[10];
        Function function = String[]::new;
        String[] names = function.apply(10);
        System.out.println(names.length);
    }
//10
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/842234.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号