Home > Java > javaTutorial > body text

Matching method of Lambda expression in Java interface

WBOY
Release: 2023-05-04 12:55:06
forward
1659 people have browsed it

1. Description

(1) Each lambda can match a given type through a specific interface. A so-called functional interface must have one and only one abstract method declaration. Each lambda expression corresponding to it must match the declaration of the abstract method. Since default methods are not abstract, feel free to add default methods to your functional interface.

(2) Any interface containing only one abstract method can be used as a lambda expression. In order for the defined interface to meet the requirements, the interface should be marked with @FunctionalInterface. If a second abstract method is defined in the interface, the compiler will notice this annotation and throw an exception.

2. Example

@FunctionalInterface
interface Converter<F, T> {
    T convert(F from);
}
 
Converter<String, Integer> converter = (from) -> Integer.valueOf(from);
Integer converted = converter.convert("123");
System.out.println(converted);    // 123
Copy after login

Note that if you do not write the @FunctionalInterface annotation, the program is also correct.

What are the characteristics of Java

1. As a representative of static object-oriented programming language, Java language implements object-oriented theory and allows programmers to perform complex programming with an elegant way of thinking.

2.Java has the characteristics of simplicity, object-oriented, distributed, security, platform independence and portability, and dynamic nature.

3. Use Java to write desktop applications, Web applications, distributed systems and embedded system applications, etc.

The above is the detailed content of Matching method of Lambda expression in Java interface. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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!