How Function Interfaces Work in Java 8?
来源:互联网 发布:php前端开发 编辑:程序博客网 时间:2024/05/17 01:28
In this post, I will use a simple example to illustrate how function interfaces work in Java 8.
1. Simple Example of Stream.filter()
The following code can be used to filter a list of strings by specifying the string's length
package com.programcreek.java8.stream; import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.stream.Stream; public class Java8Filter { public static void main(String[] args) {List<String> list = new ArrayList<String>();list.add("java");list.add("php");list.add("python");list.add("lisp");list.add("c++"); //filter functionStream<String> stream = list.stream().filter(p -> p.length() > 3);String[] arr = stream.toArray(String[]::new); System.out.println(Arrays.toString(arr));}}
2. What is Function Interfaces
A functional interface is an interface with a single abstract method. The Java API has many one-method interfaces such as Runnable
, Callable
, Comparator
, ActionListener
, etc.
Let's take a look at the signature of the filter() method of Stream:
Stream<T> filter(Predicate<? super T> predicate);
From the method signature of filter(), the lambda expression
p -> p.length() > 3
should be an instance of Predicate. The Predicate interface has one abstract method:boolean test(T t)
Because of the object-oriented feature of Java, everything should be an object. So behind the scene, the lambda expression is converted to an object of functional interface.
3. Implementation of Object of Functional Interface
You may be curious about how the object of the function interface - Predicate - look like. We can guess the possible implementation of filter() method:
Stream<T> filter(Predicate<? super T> predicate){for (each string in stream){ if(predicate.test(string)){ keep the string }else{ drop the string } return stream;}
The predicate object is converted from the given lambda expression.
There are also other functional interfaces defined. Check out the java.util.function package to see the complete list.
- How Function Interfaces Work in Java 8?
- How does a HashMap work in JAVA
- How to use interfaces in Go
- How Does Excel’s RTD Function Work ?
- How does this enqueue function work?
- The Collection Interfaces in Java
- Thinking in Java之Interfaces
- Think in java学习笔记-第8章 Interfaces
- How Java Web Servers Work
- How Java Web Servers Work
- How Java Web Servers Work
- How Java Web Servers Work
- How Java Web Servers Work
- Java 8 functional interfaces
- Java 8: Functional Interfaces
- How system calls work in Linux
- How do virtual functions work in C++?
- How Rendering Work (in WebKit and Blink)
- 1044 顺序栈基本操作的实现
- SQL Sever多表查询在vs上的应用
- JVM那些事儿(二)——垃圾回收
- 黑马程序员————Java基础部分之多态、内部类、异常
- Centos 6.4 安装erlang&rabbitmq
- How Function Interfaces Work in Java 8?
- 一个关于while(cin>>d)不得不注意的问题
- VS.NET2010水晶报表安装部署[VS2010]
- 東京喰種_经典台词中日双语2
- 数据库写入性能测试小工具
- sqlite与C++进行连接
- ListView的高级使用
- JAVA中Final的用法
- opencv CV_32FC1类型Mat的元素访问与显示