高级JAVA - 利用函数式接口实现通用的取并集/交集/差集
package com.xing.dto;import com.xing.common.utils.XDataUtil;
import lombok.Data;import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;/*** @title 两个集合的聚合数据 , 包含并集/交集/差集* @author Xingbz* @createDate 2019-7-23*/
@Data
public class GatherData<T> {/** 并集 - 新集合加旧集合的数据(去重) */private List<T> unionList;/** 交集 - 新集合和旧集合都有的数据 */private List<T> intersectList;/** 主差集 - 新集合有 , 旧集合没有的数据 */private List<T> exceptMainList;/** 次差价 - 旧集合有 , 新集合没有的数据 */private List<T> exceptMinorList;private GatherData() {unionList = new ArrayList<>();intersectList = new ArrayList<>();exceptMainList = new ArrayList<>();exceptMinorList = new ArrayList<>();}private GatherData(List<T> unionList, List<T> intersectList, List<T> exceptMainList, List<T> exceptMinorList) {this.unionList = unionList;this.intersectList = intersectList;this.exceptMainList = exceptMainList;this.exceptMinorList = exceptMinorList;}/** 遍历并集操作 */public void unionEach(Consumer<? super T> consumer) {forEach(this.unionList, consumer);}/** 遍历交集操作 */public void intersectEach(Consumer<? super T> consumer) {forEach(this.intersectList, consumer);}/** 遍历主差集操作 */public void exceptMainEach(Consumer<? super T> consumer) {forEach(this.exceptMainList, consumer);}/** 遍历次差集操作 */public void exceptMinorEach(Consumer<? super T> consumer) {forEach(this.exceptMinorList, consumer);}/** @title 遍历集合并执行相关操作* @param consumer 函数式接口实现方法*/private void forEach(List<T> list, Consumer<? super T> consumer) {for (T t : list) {consumer.accept(t);}}/*** @title 主要处理方法* @author Xingbz* @param newList 新数据集合* @param oldList 旧数据集合(一般为数据库记录)* @param function 业务转换方法(及两个集合中用来标识唯一的属性)*/public static <T, R> GatherData<T> handle(List<T> newList, List<T> oldList, Function<T, R> function) {if (XDataUtil.isEmpty(newList) || XDataUtil.isEmpty(oldList)) {return new GatherData<>();}List<R> newIdList = newList.stream().map(function).collect(Collectors.toList());//新ID集合List<R> oldIdList = oldList.stream().map(function).collect(Collectors.toList());//旧ID集合//交集 : 新集合和旧集合都有的数据(此处返回的是旧集合 , 相当于数据库中的数据集合 , 是因为这部分数据还有创建时间更新时间等)List<T> intersectList = oldList.stream().filter(ad -> newIdList.contains(function.apply(ad))).collect(Collectors.toList());//主差集 - 新集合有 , 旧集合没有的数据List<T> exceptMainList = newList.stream().filter(ad -> !oldIdList.contains(function.apply(ad))).collect(Collectors.toList());//次差价 - 旧集合有 , 新集合没有的数据List<T> exceptMinorList = oldList.stream().filter(ad -> !newIdList.contains(function.apply(ad))).collect(Collectors.toList());//并集 - 新集合加旧集合的数据(去重)List<T> unionList = XDataUtil.addAllList(intersectList, exceptMainList, exceptMinorList);return new GatherData<>(unionList, intersectList, exceptMainList, exceptMinorList);}public static void main(String[] args) {List<String> newList = new ArrayList<>();newList.add("3");newList.add("4");newList.add("5");List<String> oldList = new ArrayList<>();oldList.add("1");oldList.add("2");oldList.add("3");oldList.add("4");GatherData<String> gatherData = GatherData.handle(newList, oldList, Long::valueOf);List<String> unionList = gatherData.getUnionList();List<String> intersectList = gatherData.getIntersectList();List<String> exceptMainList = gatherData.getExceptMainList();List<String> exceptMinorList = gatherData.getExceptMinorList();System.out.println(unionList);System.out.println(intersectList);System.out.println(exceptMainList);System.out.println(exceptMinorList);}
}
实际项目中使用示例 :
//本次修改的所有用户List<User> newList = dto.getUserList();//数据库中现有的所有对象List<User> oldList = userMapper.selectDbUser(dto.getEmail());//取差集/交集 数据GatherData<User> gatherUser = GatherData.handle(newList, oldList, User :: getUserId);//主差集(本次修改新增的) 保存gatherUser.exceptMainEach(userMapper::insert);//次差集(数据库原有但本次新增去掉的) 删除gatherUser.exceptMinorEach(userMapper::remove);
高级JAVA - 利用函数式接口实现通用的取并集/交集/差集相关推荐
- 高级JAVA - 利用函数式接口实现通用的取并集/交集/差集进阶版
在前文(高级JAVA - 利用函数式接口实现通用的取并集/交集/差集)中我们实现了利用函数式接口获取交集/差集 , 但是只能全部生成 , 假如我们只需要一个交集的话 , 所有代码仍然会执行一次 . 比 ...
- 学习 Java 8 - 函数式接口 Lambda
学习 Java 8 - 函数式接口 Java 8 引入了函数式接口的概念.函数式接口其实就是只包含一个抽象方法的普通 Java 接口.在没有引入函数式接口之前,我们通常使用内部类和匿名类来实现类似的功 ...
- java中函数式接口的使用
java中函数式接口的使用 一.自己创建的函数式接口 1.1 概念 只存在一个抽象方法的接口. 1.2 使用步骤: 1.创建函数式接口,创建格式参照使用格式. 2.创建一个实例类 3.在实例类中使用函 ...
- Java之函数式接口(FunctionalInterface)
Java之函数式接口(FunctionalInterface) 1. FunctionalInterface是什么? FunctionalInterface 中文称之为 函数式接口.是Java新特性之 ...
- 【java】函数式接口和Stream
函数式接口和Stream 函数式接口和Stream 1. 函数式接口 1.1 函数式接口概述 1.2 常用函数式接口 1.3 比较器函数式接口 1.4 消费函数式接口 Consumer 1.5 生产者 ...
- Java四大函数式接口
Java8函数式接口 1. Consumer<T> 消费型接口 2. Supplier <R> 供给型接口 3. Function<T, R> 函数型接口 4. P ...
- 【Java】函数式接口与Lambda表达式
函数式接口--@FunctionalInterface Code @FunctionalInterface interface IService {void say(String message); ...
- java自定义函数式接口
⾃定义lambda接⼝流程 定义⼀个函数式接⼝ 需要标注此接⼝ @FunctionalInterface,否则万⼀团队成员在接⼝上加 了其他⽅法则容易出故障 编写⼀个⽅法,输⼊需要操做的数据和接⼝ 在 ...
- java 8 函数式接口_必看:通俗易懂地告诉你什么是java8中的“函数式接口”
花10分钟认真的看完一篇文章,或许会有意想不到的收获 java8发布已经好几年了,相信很多小伙伴都使用过java8,java8这版本带来了很多新特性,其中一个就是"函数式接口",今 ...
最新文章
- 我的学习笔记_Windows_HOOK编程 2009-12-03 11:19
- python库整理:os
- 计算机基础及ms office应用,全国计算机等级考试一级计算机基础及MS Office应用模拟练习系统...
- android Drawable.mutate()的使用
- 大航海商务助理更新1.24
- 如何将 Linq 的查询结果转为 HashSet ?
- 工作区、暂存区、版本库、远程仓库
- FFPLAY的原理(一)
- 【ElasticSearch】es 面试题 视频 笔记
- 国网的“好管家”——业务可视化管理平台
- recv 函数返回值说明
- CentOs 7源码安装 Python3
- 数据结构——图(存储结构)
- ubuntu GRUB 引导加载 Beini(WiFi破解神器) Tiny Core Linux
- CPU占用率百分百原因及解决方法
- Git首次提交代码到远程仓库时,出现fatal: unable to access ‘https://github.com/xxx/xxx.git/‘
- 运放输出端加一个小电阻的作用
- 网上GIS的参考资料网站
- PotPlayer固定播放尺寸
- 关于Python爬取网页返回521状况码的解决方案