两个list<object> 比较 得到相同数据 差异数据
package com.lizi.admin.utils.contrast;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class EntityUtil {
private static String GET_METHOD_PREFIX = "get";
public static <T> Object getFieldValue(T t, String fieldName) throws IllegalArgumentException, NoSuchMethodException, SecurityException, IllegalAccessException, InvocationTargetException{
String methodName = getGetMethodRealName(fieldName);
if(methodName == null){
throw new IllegalArgumentException("外部订单号不能为空");
}
Class<?> clazz = t.getClass();
Method getMethod = clazz.getMethod(methodName);
return getMethod.invoke(t);
}
private static String getGetMethodRealName(String fieldName){
if(fieldName == null || fieldName.length() ==0){
return null;
}
String getMethodRealName = GET_METHOD_PREFIX + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
return getMethodRealName;
}
}
package com.lizi.admin.utils.contrast;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ListUtils {
private static <T> boolean isBaseEqual(List<T> first, List<T> second){
if(first == null || second == null || first.size() == 0 || second.size() == 0){
return false;
}
return true;
}
/**
* 获取在第一个集合中但是不在第二个集合中的数据
* @param first 第一个集合
* @param second 第二个集合
* @param compareFieldName 需要比较的属性名称
* @return
* @throws IllegalArgumentException
* @throws NoSuchMethodException
* @throws SecurityException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static <T> List<T> getInFirstNotInSecondList(List<
T> first, List<T> second, String compareFieldName) throws IllegalArgumentException, NoSuchMethodException, SecurityException, IllegalAccessException, InvocationTargetException{
List<T> result = new ArrayList<>();
if(isBaseEqual(first, second)){
Map<String, String> listMap = new HashMap<>();
String temp;
for(T t : second){
temp = String.valueOf(EntityUtil.getFieldValue(t, compareFieldName));
listMap.put(temp, temp);
}
for(T t : first){
temp = String.valueOf(EntityUtil.getFieldValue(t, compareFieldName));
if(listMap.get(temp) == null){
result.add(t);
}
}
}
return result;
}
/**
* 获取两个集合中共有元素集合,通过比对指定的属性名称所对应的属性值
* @param first 集合一
* @param second 集合二
* @param compareFieldName 需要做比较的属性名称
* @return 两个集合中共有元素
* @throws IllegalArgumentException
* @throws NoSuchMethodException
* @throws SecurityException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static <T> List<T> getCommonElements(List<T> first, List<T> second,String compareFieldName) throws IllegalArgumentException, NoSuchMethodException, SecurityException, IllegalAccessException, InvocationTargetException{
List<T> result = new ArrayList<>();
if(isBaseEqual(first, second)){
Map<String, List<T>> listMap = new HashMap<>();
String temp;
for(T t : second){
temp = String.valueOf(EntityUtil.getFieldValue(t, compareFieldName));
if(listMap.get(temp) == null){
listMap.put(temp, new ArrayList<T>());
}
listMap.get(temp).add(t);
}
for(T t : first){
temp = String.valueOf(EntityUtil.getFieldValue(t, compareFieldName));
if(listMap.get(temp) != null){
result.add(t);
result.addAll(listMap.get(temp));
}
}
}
return result;
}
private static <T, E> boolean isBaseEqualOne(List<T> first, List<E> second){
if(first == null || second == null || first.size() == 0 || second.size() == 0){
return false;
}
return true;
}
/**
*
* @param first
* @param second
* @param compareFieldName
* @return
* @throws IllegalArgumentException
* @throws NoSuchMethodException
* @throws SecurityException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static <T, E> List<Object> getInFirstNotInSecondListOne(List<
T> first, List<E> second, String compareFieldName) throws IllegalArgumentException, NoSuchMethodException, SecurityException, IllegalAccessException, InvocationTargetException{
List<Object> result = new ArrayList<>();
if(isBaseEqualOne(first, second)){
Map<String, String> listMap = new HashMap<>();
String temp;
for(E e : second){
temp = String.valueOf(EntityUtil.getFieldValue(e, compareFieldName));
listMap.put(temp, temp);
}
for(T t : first){
temp = String.valueOf(EntityUtil.getFieldValue(t, compareFieldName));
if(listMap.get(temp) == null){
result.add(t);
}
}
}
return result;
}
public static <T, E> List<Object> getCommonElementsOne(List<T> first,
List<E> second,String compareFieldName) throws IllegalArgumentException, NoSuchMethodException, SecurityException, IllegalAccessException, InvocationTargetException{
List<Object> result = new ArrayList<>();
if(isBaseEqualOne(first, second)){
Map<String, List<E>> listMap = new HashMap<>();
String temp;
for(E e : second){
temp = String.valueOf(EntityUtil.getFieldValue(e, compareFieldName));
if(listMap.get(temp) == null){
listMap.put(temp, new ArrayList<E>());
}
listMap.get(temp).add(e);
}
for(T t : first){
temp = String.valueOf(EntityUtil.getFieldValue(t, compareFieldName));
if(listMap.get(temp) != null){
result.add(t);
result.addAll(listMap.get(temp));
}
}
}
return result;
}
}
package eq;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class Test {
public void aa() throws IllegalArgumentException, NoSuchMethodException, SecurityException, IllegalAccessException, InvocationTargetException{
List<User> first = new ArrayList<>();
first.add(new User("a001" , "张三"));
first.add(new User("a002" , "李四"));
first.add(new User("a003" , "王五"));
first.add(new User("a004" , "不知道"));
first.add(new User("a007" , "李大麻子"));
List<Student> second=new ArrayList<Student>();
second.add(new Student("a001","大明",3));
second.add(new Student("a002","大林",3));
second.add(new Student("a003","大哈",3));
second.add(new Student("a004","大紫",3));
second.add(new Student("a009","大3",3));
List<Object> inFirstNotInSecond = ListUtils.getInFirstNotInSecondListOne(first, second, "code");
for (int i = 0; i < inFirstNotInSecond.size(); i++) {
User u=(User) inFirstNotInSecond.get(i);
System.out.println(u.getCode()+","+u.getName());
}
System.err.println("inFirstNotInSecond: " + inFirstNotInSecond);
List<Object> bothIn = ListUtils.getCommonElementsOne(first, second, "code");
for (int i = 0; i < bothIn.size(); i++) {
Object o=bothIn.get(i);
if(o.getClass().equals(User.class)){
System.out.println("00000");
}
if(o.getClass().equals(Student.class)){
System.out.println("00001");
}
}
System.err.println("inFirstNotInSecond: " + bothIn);
List<User> listsone=new LinkedList<User>();
List<Student> liststwo=new LinkedList<Student>();
for (int i = 0; i < bothIn.size(); i++) {
Object o=bothIn.get(i);
if(o.getClass().equals(User.class)){
listsone.add((User) o);
}
if(o.getClass().equals(Student.class)){
liststwo.add((Student) o);
}
}
//比较
for(int i=0;i<listsone.size();i++){
for(int k=0;k<liststwo.size();k++){
if(liststwo.get(k).getCode().equals(listsone.get(i).getCode())){
//数据一致
break;
}
}
}
}
public static void main(String[] args) throws IllegalArgumentException, NoSuchMethodException, SecurityException, IllegalAccessException, InvocationTargetException {
Test t=new Test();
t.aa();
List<User> first = new ArrayList<>();
first.add(new User("a001" , "张三"));
first.add(new User("a002" , "李四"));
first.add(new User("a003" , "王五"));
first.add(new User("a004" , "不知道"));
first.add(new User("a007" , "李大麻子"));
System.err.println(first);
List<User> second = new ArrayList<>();
second.add(new User("a001" , "张三"));
second.add(new User("a002" , "李四"));
second.add(new User("a003" , "王五"));
second.add(new User("a004" , "不知道"));
second.add(new User("a006" , "王大麻子"));
System.err.println(second);
List<User> inFirstNotInSecond = ListUtils.getInFirstNotInSecondList(first, second, "code");
System.err.println("inFirstNotInSecond: " + inFirstNotInSecond);
List<User> bothIn = ListUtils.getCommonElements(first, second, "code");
System.err.println("bothIn: " + bothIn);
}
}
两个list<object> 比较 得到相同数据 差异数据的更多相关文章
- 转-oracle中比较两表表结构差异和数据差异的方法
oracle中比较两表表结构差异和数据差异的方法 原作者:li2008xue2008ling 出处:http://blog.csdn.net 在工作中需要完成这么一个需求:比较两个表的表 ...
- oracle中比较两表表结构差异和数据差异的方法
在工作中需要完成这么一个需求:比较两个表的表结构是否形相同,并找出差异.比较两个表中的数据是否相同,并找出差异数据? 分析:由于表结构中字段比较多,手工比较很浪费时间,而且不能保证不出错误.对于 ...
- excel 快速比对两列数据差异
excel 快速比对两列数据差异 CreateTime--2018年5月31日11:19:35 Author:Marydon 1.情景展示 找出两列数据的差异 2.具体操作 方式一:使用条件格式 ...
- 对比使用Charles和Fiddler两个工具及利用Charles抓取https数据(App)
对比使用Charles和Fiddler两个工具及利用Charles抓取https数据(App) 实验目的:对比使用Charles和Fiddler两个工具 实验对象:车易通App,易销通App 实验结果 ...
- flink-----实时项目---day07-----1.Flink的checkpoint原理分析 2. 自定义两阶段提交sink(MySQL) 3 将数据写入Hbase(使用幂等性结合at least Once实现精确一次性语义) 4 ProtoBuf
1.Flink中exactly once实现原理分析 生产者从kafka拉取数据以及消费者往kafka写数据都需要保证exactly once.目前flink中支持exactly once的sourc ...
- 为什么需要两次eval才转化为需要的JSON数据,好奇怪
为什么需要两次eval才转化为需要的JSON数据,好奇怪
- Oracle的学习二:表管理(数据类型、创建/修改表、添加/修改/删除数据、数据查询)
1.Oracle表的管理 表名和列名的命名规则: 必须以字母开头: 长度不能超过30个字符: 不能使用oracle的保留字: 只能使用如下字符:A-Z, a-z, 0-9, $, # 等. Oracl ...
- Hadoop! | 大数据百科 | 数据观 | 中国大数据产业观察_大数据门户
你正在使用过时的浏览器,Amaze UI 暂不支持. 请 升级浏览器 以获得更好的体验! 深度好文丨读完此文,就知道Hadoop了! 来源:BiThink 时间:2016-04-12 15:1 ...
- PHP中$_REQUEST中包含的数据,数据被覆盖问题
这个问题涉及到php.ini中的两个变量. variables_order = "EGPCS" variables_order 系统在定义PHP预定义变量,EGPCS 是 Envi ...
随机推荐
- const的位置与区别
转自 http://www.cnblogs.com/wucx/p/4566176.html 一个比较经典的问题——问以下两种声明的区别:1) const char * p2) char * co ...
- linux开机随笔
(1),linux开机流程: 固件是在软件与硬件之间的那部分,他们既不叫做硬件也不叫做软件, 开机自检 ,就是 在你按下开机键时,电脑就会自动检查你的硬盘 内存 cpu等器件, 那个CMOS是固 ...
- 循序渐进Python3(十一) --5-- 同源策略
一.什么是同源策略 同源策略(Same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能.它是由Netscape提出的一个著名的安全策略,现在所有的可支持javascript ...
- c++继承概念
蠢了3年了,在找工作的今天明白了何为继承……呵呵呵…… 3种继承方式:public.protected.private. 注意几点: 1.继承只能继承基类的public和protected成员 2.3 ...
- oracle 队列
Oracle 高级队列(AQ) 适用对象:初步了解oracle高级队列人群 注意事项: 序号 注意事项 1 JMS监听部分可参考官方文档: http://docs.oracle.com/cd/e128 ...
- Java实现文件压缩与解压
Java实现ZIP的解压与压缩功能基本都是使用了Java的多肽和递归技术,可以对单个文件和任意级联文件夹进行压缩和解压,对于一些初学者来说是个很不错的实例.(转载自http://www.puiedu. ...
- Python—使用__slots__限制实例的属性
如果我们想要限制实例的属性怎么办?比如,只允许对Student实例添加name和age属性. 为了达到限制的目的,Python允许在定义class的时候,定义一个特殊的__slots__变量,来限制该 ...
- C#实现JSON序列化与反序列化
1.使用 JavaScriptSerializer类实现序列化 namespace: System.Web.Script.Serialization eg: // 序列化 private string ...
- SimpleDateFormat的应用
import java.text.SimpleDateFormat;import java.util.Date;public class Main { public static void ma ...
- JQuery源码解析-- 对象的创建
使用 $("a") 返回的对象就不再是一个简单的DOM对象了,而是一个复杂的JQuery对象. 那么JQuery是怎么创建对象的. 为了便于分析,我将JQuery中复杂的代码简化了 ...