当前位置:网站首页>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
边栏推荐
- 对话龙智高级咨询顾问、Atlassian认证专家叶燕秀:Atlassian产品进入后Server时代,中国用户应当何去何从?
- C1 certification learning notes 3 -- Web Foundation
- PXE网络
- Redis publier et s'abonner
- [local differential privacy and random response code implementation] differential privacy code implementation series (13)
- %f格式符
- 左右对齐!
- Usage of database functions "recommended collection"
- Intelligent customer service track: Netease Qiyu and Weier technology play different ways
- Redis sentinel mode realizes one master, two slave and three Sentinels
猜你喜欢

力扣刷题01(反转链表+滑动窗口+LRU缓存机制)
Redis的4种缓存模式分享

AI has surpassed Dr. CS in question making?

科普达人丨一文看懂阿里云的秘密武器“神龙架构”

对话龙智高级咨询顾问、Atlassian认证专家叶燕秀:Atlassian产品进入后Server时代,中国用户应当何去何从?

go-zero微服务实战系列(九、极致优化秒杀性能)

lnx 高效搜索引擎、FastDeploy 推理部署工具箱、AI前沿论文 | ShowMeAI资讯日报 #07.04

Redis 解决事务冲突之乐观锁和悲观锁
![[Dalian University of technology] information sharing of postgraduate entrance examination and re examination](/img/06/df5a64441814c9ecfa2f039318496e.jpg)
[Dalian University of technology] information sharing of postgraduate entrance examination and re examination

numpy笔记
随机推荐
Unity脚本常用API Day03
go-zero微服务实战系列(九、极致优化秒杀性能)
Numpy notes
Redis 發布和訂閱
智能客服赛道:网易七鱼、微洱科技打法迥异
They are all talking about Devops. Do you really understand it?
从0到1建设智能灰度数据体系:以vivo游戏中心为例
Unity脚本API—Component组件
Helix Swarm中文包发布,Perforce进一步提升中国用户体验
产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现
Unity预制件Prefab Day04
CentOS 6.3 下 PHP编译安装JSON模块报错解决
%f格式符
Flutter reports an error no mediaquery widget ancestor found
[differential privacy and data adaptability] differential privacy code implementation series (XIV)
左右对齐!
Data Lake Governance: advantages, challenges and entry
大神详解开源 BUFF 增益攻略丨直播
31年前的Beyond演唱会,是如何超清修复的?
每周招聘|高级DBA年薪49+,机会越多,成功越近!