当前位置:网站首页>函数式接口,方法引用,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);
}
效果
可以根据需求自行拓展函数接口,需要注意参数类型
和返回值类型
,大多数时候更换返回值类型就可以了;
参数类型以及返回值类型影响这个函数方法在调用的时候需要传入的参数,以及响应值
边栏推荐
- Decimal, exponential
- Force button brush question 01 (reverse linked list + sliding window +lru cache mechanism)
- 文本挖掘工具的介绍[通俗易懂]
- Live broadcast preview | PostgreSQL kernel Interpretation Series II: PostgreSQL architecture
- Hexadecimal form
- Redis publier et s'abonner
- 力扣刷题01(反转链表+滑动窗口+LRU缓存机制)
- When synchronized encounters this thing, there is a big hole, pay attention!
- Implementation of macro instruction of first-order RC low-pass filter in signal processing (easy touch screen)
- odoo数据库主控密码采用什么加密算法?
猜你喜欢
中国主要城市人均存款出炉,你达标了吗?
Force button brush question 01 (reverse linked list + sliding window +lru cache mechanism)
一篇文章搞懂Go语言中的Context
Preliminary exploration of flask: WSGI
这几年爆火的智能物联网(AIoT),到底前景如何?
数据湖治理:优势、挑战和入门
Weekly recruitment | senior DBA annual salary 49+, the more opportunities, the closer success!
Helix swarm Chinese package is released, and perforce further improves the user experience in China
How to handle exceptions in multithreading?
每周招聘|高级DBA年薪49+,机会越多,成功越近!
随机推荐
unity update 协程_Unity 协程的原理
左右对齐!
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
Kubernets pod exists finalizers are always in terminating state
Enter the width!
When synchronized encounters this thing, there is a big hole, pay attention!
How to build a technical team that will bring down the company?
LeetCode 1184. Distance between bus stops -- vector clockwise and counterclockwise
【大连理工大学】考研初试复试资料分享
C1 certification learning notes 3 -- Web Foundation
Exploration and practice of eventbridge in the field of SaaS enterprise integration
MP3是如何诞生的?
Flutter reports an error no mediaquery widget ancestor found
Introduction to modern control theory + understanding
Align left and right!
hexadecimal
On the implementation plan of MySQL explain
产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现
这几年爆火的智能物联网(AIoT),到底前景如何?
深度学习 神经网络案例(手写数字识别)