当前位置:网站首页>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
边栏推荐
- Halcon knowledge: NCC_ Model template matching
- Preliminary exploration of flask: WSGI
- 怎么判断外盘期货平台正规,资金安全?
- MP3是如何诞生的?
- Width accuracy
- Solve the error of JSON module in PHP compilation and installation under CentOS 6.3
- What is the future of the booming intelligent Internet of things (aiot) in recent years?
- Hexadecimal form
- Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
- 每周招聘|高级DBA年薪49+,机会越多,成功越近!
猜你喜欢

Redis 發布和訂閱

智能客服赛道:网易七鱼、微洱科技打法迥异

微博、虎牙挺进兴趣社区:同行不同路

华为云数据库DDS产品深度赋能

近一亿美元失窃,Horizon跨链桥被攻击事件分析

科研漫画 | 联系到被试后还需要做什么?
Redis哨兵模式实现一主二从三哨兵

Analysis of nearly 100 million dollars stolen and horizon cross chain bridge attacked

LNX efficient search engine, fastdeploy reasoning deployment toolbox, AI frontier paper | showmeai information daily # 07.04

The per capita savings of major cities in China have been released. Have you reached the standard?
随机推荐
Temperature control system based on max31865
一篇文章搞懂Go语言中的Context
中国主要城市人均存款出炉,你达标了吗?
Unity脚本API—Time类
在芯片高度集成的今天,绝大多数都是CMOS器件
从0到1建设智能灰度数据体系:以vivo游戏中心为例
Quelles sont les perspectives de l'Internet intelligent des objets (aiot) qui a explosé ces dernières années?
Weekly recruitment | senior DBA annual salary 49+, the more opportunities, the closer success!
Redis哨兵模式实现一主二从三哨兵
Who the final say whether the product is good or not? Sonar puts forward performance indicators for analysis to help you easily judge product performance and performance
mysql 联合主键_Mysql 创建联合主键[通俗易懂]
Intelligent customer service track: Netease Qiyu and Weier technology play different ways
进制形式
unity update 协程_Unity 协程的原理
[differential privacy and data adaptability] differential privacy code implementation series (XIV)
numpy笔记
LeetCode 35. Search the insertion position - vector traversal (O (logn) and O (n) - binary search)
CentOS 6.3 下 PHP编译安装JSON模块报错解决
MySQL index optimization
How to rapidly deploy application software under SaaS