Home  >  Article  >  Java  >  Introduction to the use of function references in Java8

Introduction to the use of function references in Java8

黄舟
黄舟Original
2017-09-19 10:27:021539browse

This article mainly introduces you to the use of function references in the java8 learning tutorial. The article introduces it in great detail through sample code. It has certain reference learning value for everyone's study or work. Friends who need it follow below Let’s learn together.

Preface

In the previous article, we used examples to explain how to define and use lambda expressions, and compared with other languages, A special specification of lambda expressions in Java. And mentioned, lambda expressions can be further simplified to function references.

This article will introduce how to use function references. Not much to say, let’s take a look at the detailed introduction.

Types of function references

Function references are divided into the following four types:

  • Static functions, such as the parseInt function of the Integer class, can be written as Integer::parseInt

  • References to object-level functions, such as the length function of the String class , can be written as String::length

  • A reference to a function of a specific instance, such as getValue of an instance named expensiveTransaction, written as expensiveTransaction::getValue

  • Constructor reference

Static function

For example:


Function<String, Integer> stringToInteger = (String s) -> Integer.parseInt(s);

can be written as:


Function<String, Integer> stringToInteger = Integer::parseInt;

Reference of object level function


BiPredicate<List<String>, String> contains =
(list, element) -> list.contains(element);

can be written as:


BiPredicate<List<String>, String> contains = List::contains;

Constructor reference

Let's take another example of a constructor. First, define a functional interface, and the only method get returns an object of the specified type.


@FunctionalInterface
public interface Supplier<T> {
 T get();
}


Supplier<TantanitReader> constructor = () -> new TantanitReader();
TantanitReader tantanitReader = constructor.get();

The above code uses the lambda expression new and returns a new object, making the constructor variable a reference to the constructor.

is equivalent to the following function reference:


Supplier<TantanitReader> constructor2 = TantanitReader::new;
TantanitReader tantanitReader2 = constructor2.get();

The above examples are all without parameters. The following still uses the constructor as an example to introduce parameters. Situation:


public TantanitReader(String loginName) {
 this.loginName = loginName;
}


Function<String,TantanitReader> constructor3 = (loginName) -> new TantanitReader(loginName);
TantanitReader tantanitReader3 = constructor3.apply("jack");

Function<String,TantanitReader> constructor4 = TantanitReader::new;
TantanitReader tantanitReader4 = constructor4.apply("jack");
TantanitReader tantanitReader5 = constructor4.apply("tom");

At this time, since the function has only one parameter, you can use the Function interface that comes with Java, which The functions that actually work are as follows:


R apply(T t);

The function is to return a result based on a parameter. From this we can use constructor4 and the corresponding function reference constructor5.

Summary

Using function references can not only simplify lambda expressions, but also semantically focus more on the method name, that is, what is to be done, and the abstraction level is more Close to human cognition. Therefore, function references should be used whenever possible.

Summarize

The above is the detailed content of Introduction to the use of function references in Java8. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn