当前位置:网站首页>Functional interface, method reference, list collection sorting gadget implemented by lambda
Functional interface, method reference, list collection sorting gadget implemented by lambda
2022-07-04 15:25:00 【Humanoid bug maker 9527】
1. stay Java8 A functional interface is introduced in Consumer The object of , This object can pass methods as parameters .
About lambda A tutorial for
/** * Collection sorting */
@AllArgsConstructor
public class CollSort {
// Sorting factor
private final Long order;
public final static CollSort DESC = new CollSort(-1L);
public final static CollSort ASC = new CollSort(1L);
// Sort by number
public <E> Collection<E> sort(Collection<E> collection, ProcessorNumber<E> getParam) {
return collection.stream()
.filter(Objects::nonNull)
.sorted((o1, o2) -> {
// The incoming abstract method is executed here , And pass in the parameters required by the function method ,
// getParam The return value of has been determined in the abstract function method
long result = Long.parseLong(String.valueOf(getParam.accept(o1))) - Long.parseLong(String.valueOf(getParam.accept(o2)));
result = result * order; // Ranking coefficient intervention
return result == 0 ? 0 : result < 0 ? -1 : 1;
})
.collect(Collectors.toList());
}
// Sort by time
public <E> Collection<E> sort(Collection<E> collection, ProcessorDate<E> getParam) {
return collection.stream()
.filter(Objects::nonNull) // Invalid data filtering
.sorted((o1, o2) -> {
long t1 = getParam.accept(o1).getTime(); // Here are abstract methods getParam The return value of has been determined in the abstract function method
long t2 = getParam.accept(o2).getTime();
long result = t1 - t2;
result = result * order; // Ranking coefficient intervention
return result == 0 ? 0 : result < 0 ? -1 : 1;
})
.collect(Collectors.toList());
}
}
@FunctionalInterface
interface ProcessorDate<E> {
/** * Abstract function method to obtain the time attribute of the object * * @param t object * @return The time property of the object */
Date accept(E t);
}
@FunctionalInterface
interface ProcessorNumber<E> {
/** * Abstract function method to obtain the numerical attributes of an object * * @param t object * @return Numerical properties of objects */
Number accept(E t);
}
Use
public static void main(String[] args) {
SortTest sortTest = new SortTest();
ArrayList<User> users = new ArrayList<>();
users.add(new User("zz", 14,new Date()));
users.add(new User("zz", 17,new Date(System.currentTimeMillis()+1000000)));
users.add(new User("zz", 11,new Date(System.currentTimeMillis()+4000000)));
users.add(new User("zz", 15,new Date(System.currentTimeMillis()+9000000)));
users.add(new User("zz", 21,new Date(System.currentTimeMillis()+8000000)));
users.add(new User("zz", 7,new Date(System.currentTimeMillis()+3000000)));
sortTest.test(users);
}
public void test(List<User> users) {
// Just call ordinary tool methods , The difference is that the parameter is a method , You can also directly reference methods
Collection<User> ageSort = CollSort.ASC.sort(users, User::getAge);
System.out.println(" Age ranking =========================");
ageSort.forEach(System.out::println);
System.out.println(" Birthday sorting ======================");
Collection<User> timeSort = CollSort.DESC.sort(users, User::getBirthDay);
timeSort.forEach(System.out::println);
}
effect
You can expand the function interface according to your needs , We need to pay attention to Parameter type
and return type
, Most of the time, it is enough to change the return value type ;
Parameter types and return value types affect the parameters that need to be passed in when calling this function method , And response value
边栏推荐
- js平铺数据查找叶子节点
- 浮点数如何与0进行比较?
- 深度学习 神经网络的优化方法
- C implementation defines a set of intermediate SQL statements that can be executed across libraries
- Techsmith Camtasia Studio 2022.0.2屏幕录制软件
- 直播预告 | PostgreSQL 内核解读系列第二讲:PostgreSQL 体系结构
- MYSQL索引优化
- Quelles sont les perspectives de l'Internet intelligent des objets (aiot) qui a explosé ces dernières années?
- Force button brush question 01 (reverse linked list + sliding window +lru cache mechanism)
- TechSmith Camtasia studio 2022.0.2 screen recording software
猜你喜欢
【大连理工大学】考研初试复试资料分享
Detailed explanation of MySQL composite index (multi column index) use and optimization cases
都在说DevOps,你真正了解它吗?
Data Lake Governance: advantages, challenges and entry
The per capita savings of major cities in China have been released. Have you reached the standard?
Unity script lifecycle day02
Weekly recruitment | senior DBA annual salary 49+, the more opportunities, the closer success!
They are all talking about Devops. Do you really understand it?
Optimization method of deep learning neural network
Intelligent customer service track: Netease Qiyu and Weier technology play different ways
随机推荐
[differential privacy and data adaptability] differential privacy code implementation series (XIV)
31年前的Beyond演唱会,是如何超清修复的?
Unity脚本介绍 Day01
【大连理工大学】考研初试复试资料分享
Unity预制件Prefab Day04
js平铺数据查找叶子节点
Width accuracy
%S format character
浮点数如何与0进行比较?
How did the beyond concert 31 years ago get super clean and repaired?
.Net 应用考虑x64生成
Ffmpeg Visual Studio development (IV): audio decoding
MySQL组合索引(多列索引)使用与优化案例详解
Preliminary exploration of flask: WSGI
数据湖治理:优势、挑战和入门
hexadecimal
MySQL learning notes - data type (numeric type)
【学习笔记】拟阵
. Net applications consider x64 generation
干货 | fMRI标准报告指南新鲜出炉啦,快来涨知识吧