当前位置:网站首页>函数式接口,方法引用,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);
}
效果
可以根据需求自行拓展函数接口,需要注意参数类型和返回值类型,大多数时候更换返回值类型就可以了;
参数类型以及返回值类型影响这个函数方法在调用的时候需要传入的参数,以及响应值
边栏推荐
- Implementation of macro instruction of first-order RC low-pass filter in signal processing (easy touch screen)
- Temperature control system based on max31865
- 在芯片高度集成的今天,绝大多数都是CMOS器件
- Redis哨兵模式实现一主二从三哨兵
- 进制形式
- AI has surpassed Dr. CS in question making?
- Unity update process_ Principle of unity synergy
- 基于MAX31865的温度控制系统
- Redis 发布和订阅
- [differential privacy and data adaptability] differential privacy code implementation series (XIV)
猜你喜欢

深度学习 神经网络案例(手写数字识别)

Memory management summary

Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example

直播预告 | PostgreSQL 内核解读系列第二讲:PostgreSQL 体系结构

Dialogue with ye Yanxiu, senior consultant of Longzhi and atlassian certification expert: where should Chinese users go when atlassian products enter the post server era?

Introduction to modern control theory + understanding

Quelles sont les perspectives de l'Internet intelligent des objets (aiot) qui a explosé ces dernières années?

Unity脚本常用API Day03

這幾年爆火的智能物聯網(AIoT),到底前景如何?

中国主要城市人均存款出炉,你达标了吗?
随机推荐
都在说DevOps,你真正了解它吗?
They are all talking about Devops. Do you really understand it?
odoo数据库主控密码采用什么加密算法?
Implementation of web chat room
Partial modification - progressive development
AI has surpassed Dr. CS in question making?
MP3是如何诞生的?
Shell 编程基础
Introduction to asynchronous task capability of function calculation - task trigger de duplication
Quick introduction to automatic control principle + understanding
Redis 发布和订阅
Intelligent customer service track: Netease Qiyu and Weier technology play different ways
[learning notes] matroid
深度学习 神经网络的优化方法
selenium 浏览器(2)
Exploration and practice of eventbridge in the field of SaaS enterprise integration
进制乱炖
每周招聘|高级DBA年薪49+,机会越多,成功越近!
宽度与对齐
浮点数如何与0进行比较?