java8 lamda表达式之方法引用


消费型接口函数

Consumer<String> con2 = ps::println;

// 情况一:对象 :: 实例方法
//Consumer中的void accept(T t)
//PrintStream中的void println(T t)
@Test
public void test1() {
   Consumer<String> con1 = str -> System.out.println(str);
   con1.accept("北京");

   System.out.println("*******************");
   PrintStream ps = System.out;
   Consumer<String> con2 = ps::println;
   con2.accept("beijing");
}

供给型函数Supplier

Supplier<String> sup2 = emp::getName;

//Supplier中的T get()
//Employee中的String getName()
@Test
public void test2() {
   Employee emp = new Employee(1001,"Tom",23,5600);
   Supplier<String> sup1 = () -> emp.getName();
   System.out.println(sup1.get());
   System.out.println("*******************");
   Supplier<String> sup2 = emp::getName;
   System.out.println(sup2.get());

}

 Comparator<T>

Comparator<Integer> com2 = Integer::compare;

// 情况二:类 :: 静态方法
//Comparator中的int compare(T t1,T t2)
//Integer中的int compare(T t1,T t2)
@Test
public void test3() {
   Comparator<Integer> com = new Comparator<Integer>() {
      @Override
      public int compare(Integer o1, Integer o2) {
         return Integer.compare(o1,o2);
      }
   };
   System.out.println("*******************");
   Comparator<Integer> com1 = (t1,t2) -> Integer.compare(t1,t2);
   System.out.println(com1.compare(12,21));

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

   Comparator<Integer> com2 = Integer::compare;
   System.out.println(com2.compare(12,3));

}

Function

//Function中的R apply(T t)
//Math中的Long round(Double d)
@Test
public void test4() {
   Function<Double,Long> func = new Function<Double, Long>() {
      @Override
      public Long apply(Double d) {
         return Math.round(d);
      }
   };

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

   Function<Double,Long> func1 = d -> Math.round(d);
   System.out.println(func1.apply(12.3));

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

   Function<Double,Long> func2 = Math::round;
   System.out.println(func2.apply(12.6));
}

BiPredicate

//BiPredicate中的boolean test(T t1, T t2);
//String中的boolean t1.equals(t2)
@Test
public void test6() {
   BiPredicate<String,String> pre = new BiPredicate<String, String>() {
      @Override
      public boolean test(String s, String s2) {
         return s.equals(s2);
      }
   };

   System.out.println("*******************");
   BiPredicate<String,String> pre1 = (s1,s2) -> s1.equals(s2);
   System.out.println(pre1.test("abc","abc"));

   System.out.println("*******************");
   BiPredicate<String,String> pre2 = String :: equals;
   System.out.println(pre2.test("abc","abd"));
}

// Function中的R apply(T t)

// Function中的R apply(T t)
// Employee中的String getName();
@Test
public void test7() {
   Employee employee = new Employee(1001, "Jerry", 23, 6000);

   Function<Employee,String> func = new Function<Employee, String>() {
      @Override
      public String apply(Employee employee) {
         return employee.getName();
      }
   };
   System.out.println(func.apply(employee));


   System.out.println("*******************");
   Function<Employee,String> func1 = e -> e.getName();
   System.out.println(func1.apply(employee));

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


   Function<Employee,String> func2 = Employee::getName;
   System.out.println(func2.apply(employee));


}

class Employee {

   private int id;
   private String name;
   private int age;
   private double salary;

   public int getId() {
      return id;
   }

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

   public String getName() {
      return name;
   }

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

   public int getAge() {
      return age;
   }

   public void setAge(int age) {
      this.age = age;
   }

   public double getSalary() {
      return salary;
   }

   public void setSalary(double salary) {
      this.salary = salary;
   }

   public Employee() {
      System.out.println("Employee().....");
   }

   public Employee(int id) {
      this.id = id;
      System.out.println("Employee(int id).....");
   }

   public Employee(int id, String name) {
      this.id = id;
      this.name = name;
   }

   public Employee(int id, String name, int age, double salary) {

      this.id = id;
      this.name = name;
      this.age = age;
      this.salary = salary;
   }

   @Override
   public String toString() {
      return "Employee{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", salary=" + salary + '}';
   }

   @Override
   public boolean equals(Object o) {
      if (this == o)
         return true;
      if (o == null || getClass() != o.getClass())
         return false;

      Employee employee = (Employee) o;

      if (id != employee.id)
         return false;
      if (age != employee.age)
         return false;
      if (Double.compare(employee.salary, salary) != 0)
         return false;
      return name != null ? name.equals(employee.name) : employee.name == null;
   }

   @Override
   public int hashCode() {
      int result;
      long temp;
      result = id;
      result = 31 * result + (name != null ? name.hashCode() : 0);
      result = 31 * result + age;
      temp = Double.doubleToLongBits(salary);
      result = 31 * result + (int) (temp ^ (temp >>> 32));
      return result;
   }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值