Home > Java > javaTutorial > body text

Introduction to the use of function references in Java8

黄舟
Release: 2017-09-19 10:27:02
Original
1565 people have browsed it

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);
Copy after login

can be written as:


Function<String, Integer> stringToInteger = Integer::parseInt;
Copy after login

Reference of object level function


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

can be written as:


BiPredicate<List<String>, String> contains = List::contains;
Copy after login

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();
}
Copy after login


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

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();
Copy after login

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;
}
Copy after login


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");
Copy after login

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);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!