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!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.