当前位置:网站首页>函数式接口,方法引用,Lambda实现的List集合排序小工具
函数式接口,方法引用,Lambda实现的List集合排序小工具
2022-07-04 14:30:00 【人形bug制造机9527】
1.在Java8中引入了一个函数式接口Consumer的对象,该对象可以把方法作为参数进行传递。
关于lambda的教程
/** * 集合排序 */
@AllArgsConstructor
public class CollSort {
// 排序因子
private final Long order;
public final static CollSort DESC = new CollSort(-1L);
public final static CollSort ASC = new CollSort(1L);
// 根据数字排序
public <E> Collection<E> sort(Collection<E> collection, ProcessorNumber<E> getParam) {
return collection.stream()
.filter(Objects::nonNull)
.sorted((o1, o2) -> {
// 这里执行传入的抽象方法,并传入函数方法需要的参数,
// getParam的返回值已经在抽象函数方法中确定了
long result = Long.parseLong(String.valueOf(getParam.accept(o1))) - Long.parseLong(String.valueOf(getParam.accept(o2)));
result = result * order; // 排序系数介入
return result == 0 ? 0 : result < 0 ? -1 : 1;
})
.collect(Collectors.toList());
}
// 根据时间排序
public <E> Collection<E> sort(Collection<E> collection, ProcessorDate<E> getParam) {
return collection.stream()
.filter(Objects::nonNull) // 无效数据过滤
.sorted((o1, o2) -> {
long t1 = getParam.accept(o1).getTime(); // 这里抽象方法getParam的返回值已经在抽象函数方法中确定了
long t2 = getParam.accept(o2).getTime();
long result = t1 - t2;
result = result * order; // 排序系数介入
return result == 0 ? 0 : result < 0 ? -1 : 1;
})
.collect(Collectors.toList());
}
}
@FunctionalInterface
interface ProcessorDate<E> {
/** * 获取对象时间属性的抽象函数方法 * * @param t 对象 * @return 对象的时间属性 */
Date accept(E t);
}
@FunctionalInterface
interface ProcessorNumber<E> {
/** * 获取对象数字属性的抽象函数方法 * * @param t 对象 * @return 对象的数字属性 */
Number accept(E t);
}
使用
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) {
// 就普通的工具方法调用,区别就是参数是一个方法,也可以直接引用方法
Collection<User> ageSort = CollSort.ASC.sort(users, User::getAge);
System.out.println("年龄排序=========================");
ageSort.forEach(System.out::println);
System.out.println("生日排序排序======================");
Collection<User> timeSort = CollSort.DESC.sort(users, User::getBirthDay);
timeSort.forEach(System.out::println);
}
效果
可以根据需求自行拓展函数接口,需要注意参数类型和返回值类型,大多数时候更换返回值类型就可以了;
参数类型以及返回值类型影响这个函数方法在调用的时候需要传入的参数,以及响应值
边栏推荐
- 力扣刷题01(反转链表+滑动窗口+LRU缓存机制)
- Introduction to asynchronous task capability of function calculation - task trigger de duplication
- Width accuracy
- 每周招聘|高级DBA年薪49+,机会越多,成功越近!
- 输入宽度!
- LNX efficient search engine, fastdeploy reasoning deployment toolbox, AI frontier paper | showmeai information daily # 07.04
- MySQL federated primary key_ MySQL creates a federated primary key [easy to understand]
- 十六进制
- 进制形式
- Numpy notes
猜你喜欢

科研漫画 | 联系到被试后还需要做什么?

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

微博、虎牙挺进兴趣社区:同行不同路
Redis哨兵模式实现一主二从三哨兵

MP3是如何诞生的?

When synchronized encounters this thing, there is a big hole, pay attention!

Guitar Pro 8win10最新版吉他学习 / 打谱 / 创作

31年前的Beyond演唱会,是如何超清修复的?

深度学习 网络正则化

Leecode learning notes - Joseph problem
随机推荐
[Dalian University of technology] information sharing of postgraduate entrance examination and re examination
PXE网络
从0到1建设智能灰度数据体系:以vivo游戏中心为例
Helix swarm Chinese package is released, and perforce further improves the user experience in China
Luo Gu - some interesting questions
AI has surpassed Dr. CS in question making?
对话龙智高级咨询顾问、Atlassian认证专家叶燕秀:Atlassian产品进入后Server时代,中国用户应当何去何从?
mysql 联合主键_Mysql 创建联合主键[通俗易懂]
Unity动画Animation Day05
Live broadcast preview | PostgreSQL kernel Interpretation Series II: PostgreSQL architecture
MySQL学习笔记——数据类型(数值类型)
Align left and right!
Redis publish and subscribe
【读书会第十三期】 音频文件的封装格式和编码格式
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
力扣刷题01(反转链表+滑动窗口+LRU缓存机制)
【学习笔记】拟阵
I plan to teach myself some programming and want to work as a part-time programmer. I want to ask which programmer has a simple part-time platform list and doesn't investigate the degree of the receiv
Korean AI team plagiarizes shock academia! One tutor with 51 students, or plagiarism recidivist
LeetCode 1184. 公交站间的距离 ---vector顺逆时针