原创转载请注明出处:http://agilestyle.iteye.com/blog/2424849
Examples of lambdas and method reference equivalents
There are three main kinds of method references:
1. A method reference to a static method
(for example, the method parseInt of Integer, written Integer::parseInt)
2. A method reference to an instance method of an arbitrary type
(for example, the method length of a String, written String::length)
3. A method reference to an instance method of an existing object
(for example, suppose you have a local variable expensiveTransaction that holds an object of type Transaction, which supports an instance method getValue; you can write expensiveTransaction::getValue)
e.g.
package org.fool.java8; import java.util.function.BiFunction; import java.util.function.BiPredicate; import java.util.function.Function; import java.util.function.Supplier; public class MethodReferenceTest { public static void main(String[] args) { /* * A method reference to a static method */ Function<String, Integer> function1 = Integer::parseInt; Integer result1 = function1.apply("123"); System.out.println(result1); /* * A method reference to an instance method of an arbitrary type */ BiPredicate<String, String> predicate = String::endsWith; boolean result2 = predicate.test("hello", "world"); System.out.println(result2); /* * A method reference to an instance method of an existing object */ Person person = new Person("zhangsan", 18); Supplier<String> result3 = person::getName; System.out.println(result3.get()); /** * BiFunction只接收 2 个参数 */ BiFunction<String, Integer, Person> function = Person::new; Person person1 = function.apply("zhangsan", 18); System.out.println(person1); /** * TriFunction 自定义实现接收 3 个参数 */ TriFunction<String, Integer, String, Person> triFunction = Person::new; Person person2 = triFunction.apply("lisi", 28, "female"); System.out.println(person2); } @FunctionalInterface public interface TriFunction<T, U, O, R> { R apply(T t, U u, O o); } private static class Person { private String name; private Integer age; private String sex; public Person(String name, Integer age) { this.name = name; this.age = age; } public Person(String name, Integer age, String sex) { this.name = name; this.age = age; this.sex = sex; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", sex='" + sex + '\'' + '}'; } } }
Console Output
123 false zhangsan Person{name='zhangsan', age=18, sex='null'} Person{name='lisi', age=28, sex='female'}
相关推荐
当Lambda表达式仅仅是对某个现有方法的调用时,Java 8允许使用更加简洁的方法引用(Method Reference)。方法引用可以用更简单的方式表示Lambda表达式,提高代码的可读性。 方法引用的语法形式如下: ```java ...
Lambda表达式还能与方法引用(method reference)结合使用,当Lambda表达式体恰好等于某个现有方法的实现时,可以直接引用该方法。例如: ```java String[] words = {"hello", "world"}; Arrays.sort(words, String...
Java 8还引入了方法引用(Method Reference),它是一种更简洁的方式来引用已有的方法,代替Lambda表达式。例如,`Arrays.sort(names, String::compareTo)` 使用了方法引用来排序字符串列表。 另外,Optional类也是...
Runnable myRunnable = () -> System.out.println("Lambda expression executed"); myRunnable.run(); ``` 7. **接口与多态性** 接口是多态性的关键,因为它允许不同类的对象通过实现同一接口来共享相同的行为...
Lambda表达式(Lambda Expression)提供了一种表示内联代码块的方式,常用于编写简洁的委托和事件处理器。 属性(Property)用于访问和设置类的私有字段值。索引器(Indexer)使类的实例能以类似数组的方式被索引。...
Python参考手册,官方正式版参考手册,chm版。以下摘取部分内容:Navigation index modules | next | Python » 3.6.5 Documentation » Python Documentation contents What’s New in Python ...