案例1:
将集合里面的对象根据不同的时间粒度转为对应的月末,季末,年末
集合里面的对象未知,且字段名称未知,
调用方法:
public static void main(String[] args) {List<InvoicingNewRi> list = new ArrayList<>(16);buildLastDayOfDateLevel(list,DateLevelEnum.MONTH, InvoicingNewRi::getBizDate, (item, num) -> {item.setBizDate(num);});}
回调函数 实现
/*** 将日期转为月末,季末,年末* @param list* @param dateLevelEnum* @param function* @param consumer* @author: e-Yuanhui.Ji* @date: 2023/8/3 17:37*/public static <T> void buildLastDayOfDateLevel(List<T> list,DateLevelEnum dateLevelEnum, Function<T, LocalDate> function,BiConsumer<T, LocalDate> consumer) {if (DateLevelEnum.DAY.equals(dateLevelEnum)) {return;}for (T t : list) {switch (dateLevelEnum) {case MONTH:LocalDate month = LocalDateUtil.getLastDayOfMonth(function.apply(t));consumer.accept(t,month);break;case QUARTER:LocalDate quarter = LocalDateUtil.getLastDayOfQuarter(function.apply(t));consumer.accept(t,quarter);break;case YEAR:LocalDate year = LocalDateUtil.getLastDayOfYear(function.apply(t));consumer.accept(t,year);break;default:}}}
ps:此方法最后使用以下方式实现了:
不同日期粒度封装到getLastDayofDateLevel里面进行实现了
list.forEach(item-> item.setBizDate(BusinessUtil.getLastDayOfDateLevel(item.getBizDate(),dateLevelEnum)));
案例2:
对学生的各个学科进行统计,在前10名的单课目上打上厉害标识,在后10名打卡需要加油标识
主方法
public static void main(String[] args) {ArrayList<KljytSummary> list = new ArrayList<>(40);for (int i = 0; i < 15; i++) {list.add(new KljytSummary("张三"+i, 1+i,2+i,3+i,4*i,5*i,6+i,i+i));}// 默认以大定单数builSort(list, KljytSummary::get语文, (item, num) -> {item.set语文排名(num);});builSort(list, KljytSummary::get数学, (item, num) -> {item.set数学排名(num);});builSort(list, KljytSummary::get英语, (item, num) -> {item.set英语排名(num);});builSort(list, KljytSummary::get政治, (item, num) -> {item.set政治排名(num);});builSort(list, KljytSummary::get化学, (item, num) -> {item.set化学排名(num);});builSort(list, KljytSummary::get生物, (item, num) -> {item.set生物排名(num);});builSort(list, KljytSummary::get物理, (item, num) -> {item.set物理排名(num);});list.forEach(System.out::println);}
i学生类
@Data@AllArgsConstructorpublic static class KljytSummary {public KljytSummary(){}public KljytSummary(String name, Integer 语文, Integer 数学, Integer 英语, Integer 化学, Integer 物理, Integer 生物, Integer 政治) {this.name = name;this.语文 = 语文;this.数学 = 数学;this.英语 = 英语;this.化学 = 化学;this.物理 = 物理;this.生物 = 生物;this.政治 = 政治;}private String name;// 前百分之10在排名里面填入厉害// 后百分之10填入垃圾private Integer 语文;private String 语文排名;private Integer 数学;private String 数学排名;private Integer 英语;private String 英语排名;private Integer 化学;private String 化学排名;private Integer 物理;private String 物理排名;private Integer 生物;private String 生物排名;private Integer 政治;private String 政治排名;}
实现
/*** @param list 遍历的集* @param mapper 得出需要排名的字段* @param consumer 将结果设置到对应的优劣字段中* @author: e-Yuanhui.Ji* @date: 2022/12/13 09:00*/public static void builSort(List<KljytSummary> list, Function<KljytSummary, Integer> mapper,BiConsumer<KljytSummary, String> consumer) {if (CollectionUtils.isEmpty(list)) {return;}list.sort(Comparator.comparingDouble(item -> 0 - mapper.apply(item)));int size = list.size();// 四舍五入long round = Math.round(size * 0.1);// 点赞for (int i = 0; i < round; i++) {KljytSummary kljytSummaryBO = list.get(i);Number apply = mapper.apply(kljytSummaryBO);if (apply != null) {consumer.accept(list.get(i), "厉害");}}// 加油for (int i = size - 1; i >= size - round; i--) {KljytSummary kljytSummaryBO = list.get(i);Number apply = mapper.apply(kljytSummaryBO);if (apply != null) {consumer.accept(list.get(i), "需要加油");}}}