Java--方法引用和构造器引用,数组引用的使用

使用情况:当传递的Lambda体的操作,已经有实现的方法了,可以使用方法引用
使用格式:类(或对象)::方法名
     1:对象::非静态方法
     2:类::静态方法
     3:类::非静态方法

/**
 * 描述
 * @version 1.0
 * @date 2022/05/14 22:43:35
 */
public class Student {

    private Integer id;
    private String name;

    public Student() {
    }

    public Student(Integer id) {
        this.id = id;
    }

    public Student(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}
/**
     * 使用方法引用的方式
     * 1:对象::非静态方法
     */
@Test
public void test1() {

  Consumer<String> consumer = System.out::println;
  consumer.accept("测试方法引用");

  System.out.println("**************************");
  Student student = new Student(1, "李四");
  MyGet<String> name = student::getName;
  System.out.println(name.get());
}
运行结果:
  测试方法引用
  **************************
  李四
  
/**
 * 自定义函数式接口,只有一个抽象方法
 *
 * @version 1.0
 * @date 2022/05/14 19:21:05
 */
@FunctionalInterface
public interface MyInterface2<T> {
    T add(T a, T b);
}
/**
     * 2:类::静态方法
     */
@Test
public void test2() {
  MyInterface2<Integer> myInterface = Integer::sum;
  Integer add = myInterface.add(1, 2);
  System.out.println(add);

  System.out.println("**************************");

  Comparator<Integer> comparator = Integer::compare;
  int com = comparator.compare(12, 23);
  System.out.println(com);
}
运行结果:
  3
  **************************
  -1
/**
     * 3:类::静态方法
     */
@Test
public void test3() {
  Function<Double, Long> fun1 = d -> Math.round(d);
  Long apply = fun1.apply(12.4);
  System.out.println(apply);

  System.out.println("**************************");

  Function<Double, Long> fun2 = Math::round;
  Long apply1 = fun2.apply(13.3);
  System.out.println(apply1);
}
运行结果:
  12
  **************************
  13
/**
     * 4:类::实例方法
     */
@Test
public void test4() {
  Comparator<String> comparator = (s1, s2) -> s1.compareTo(s2);
  int compare = comparator.compare("abc", "abd");
  System.out.println(compare);

  System.out.println("**************************");

  Comparator<String> comparator1 = String::compareTo;
  int compare1 = comparator1.compare("abe", "abd");
  System.out.println(compare1);
}
运行结果:
  -1
  **************************
  1
/**
     * 5:类::非静态方法
     */
@Test
public void test5() {
  Student student = new Student(1, "Tom");
  Function<Student, String> function = Student::getName;
  String name = function.apply(student);
  System.out.println(name);
}
运行结果:
	Tom
/**
     * 构造器引用
     */
@Test
public void test6() {
  //空参构造器
  Supplier<Student> supplier = Student::new;
  Student student = supplier.get();
  System.out.println(student);

  System.out.println("**************************");
  //只有id的构造器
  Function<Integer, Student> function = Student::new;
  Student student1 = function.apply(1001);
  System.out.println(student1);

  System.out.println("**************************");
  //两个参数的构造器
  BiFunction<Integer, String, Student> biFunction = Student::new;
  Student jerry = biFunction.apply(1002, "Jerry");
  System.out.println(jerry);
}
运行结果:
  Student{id=null, name='null'}
  **************************
  Student{id=1001, name='null'}
  **************************
  Student{id=1002, name='Jerry'}
/**
     * 数组引用
     */
@Test
public void test7() {

  Function<Integer, String[]> function = String[] :: new;
  String[] apply = function.apply(10);
  System.out.println(Arrays.toString(apply));

}
运行结果:
  [null, null, null, null, null, null, null, null, null, null]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值