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> 比较 得到相同数据 差异数据的更多相关文章

  1. 转-oracle中比较两表表结构差异和数据差异的方法

    oracle中比较两表表结构差异和数据差异的方法 原作者:li2008xue2008ling  出处:http://blog.csdn.net       在工作中需要完成这么一个需求:比较两个表的表 ...

  2. oracle中比较两表表结构差异和数据差异的方法

    在工作中需要完成这么一个需求:比较两个表的表结构是否形相同,并找出差异.比较两个表中的数据是否相同,并找出差异数据?    分析:由于表结构中字段比较多,手工比较很浪费时间,而且不能保证不出错误.对于 ...

  3. excel 快速比对两列数据差异

      excel 快速比对两列数据差异 CreateTime--2018年5月31日11:19:35 Author:Marydon 1.情景展示 找出两列数据的差异 2.具体操作 方式一:使用条件格式 ...

  4. 对比使用Charles和Fiddler两个工具及利用Charles抓取https数据(App)

    对比使用Charles和Fiddler两个工具及利用Charles抓取https数据(App) 实验目的:对比使用Charles和Fiddler两个工具 实验对象:车易通App,易销通App 实验结果 ...

  5. 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 ...

  6. 为什么需要两次eval才转化为需要的JSON数据,好奇怪

    为什么需要两次eval才转化为需要的JSON数据,好奇怪

  7. Oracle的学习二:表管理(数据类型、创建/修改表、添加/修改/删除数据、数据查询)

    1.Oracle表的管理 表名和列名的命名规则: 必须以字母开头: 长度不能超过30个字符: 不能使用oracle的保留字: 只能使用如下字符:A-Z, a-z, 0-9, $, # 等. Oracl ...

  8. Hadoop! | 大数据百科 | 数据观 | 中国大数据产业观察_大数据门户

        你正在使用过时的浏览器,Amaze UI 暂不支持. 请 升级浏览器 以获得更好的体验! 深度好文丨读完此文,就知道Hadoop了! 来源:BiThink 时间:2016-04-12 15:1 ...

  9. PHP中$_REQUEST中包含的数据,数据被覆盖问题

    这个问题涉及到php.ini中的两个变量. variables_order = "EGPCS" variables_order 系统在定义PHP预定义变量,EGPCS 是 Envi ...

随机推荐

  1. windbg学习---!thread和.thread

    !thread扩展显示目标系统中线程包括ETHREAD块在内的摘要信息.该命令只能在内核模式调试下使用 !thread [-p] [-t] [Address [Flags]] -p 显示拥有该线程的进 ...

  2. js 滚动 学习

    // s:= 'document.getElementsByName(''seccodeverify'')[0].scrollTop=100;'; // s:='scrollTo(0,document ...

  3. JDBC性能优化点

    1). 指定数据库连接池中初始化连接数的个数

  4. <一>Angular.js学习

    angular.module(name, [a], [b]);  // angular.module()创建.获取.注册angular中的模块 name:字符串类型,代表模块的名称: a:字符串的数组 ...

  5. 敏捷软件开发vs传统软件开发

    摘要 本文介绍了传统软件开发(着重介绍了传统软件开发中常用的瀑布模型)和敏捷软件开发,以及敏捷开发和传统开发的对比. 一.传统软件开发 比较常用的几种传统软件开发方法:瀑布式开发.迭代式开发.螺旋开发 ...

  6. 单位换算(格式化十进制数-B),获取时间工具类CommenUtil

    package com.example.administrator.filemanager.utils;import java.text.DecimalFormat;import java.text. ...

  7. The remote name could not be resolved问题的解决方法

    网站如果绑定了代理ip,内部跳转的时候,就会报The remote name could not be resolved错误,这个错误很难排查,网上也没有多少可参考的例子 现在记录下解决方法,以备参考 ...

  8. 解决跑twoBitToFa时出现“/admin/exe/linux.x86_64/twoBitToFa: Permission denied”的问题

    出现这种问题时,一般要加上以下命令: chmod ugo+x ./admin/exe/linux.x86_64/twoBitToFa 运行成功后,再将twobit格式转化为fa格式 ./admin/e ...

  9. h5 js 图片预览并判断 ajax上传

    //建立一個可存取到該file的url function getObjectURL(file) { var url = null; if (window.createObjectURL != unde ...

  10. ubuntu14.04 安装系统

    p { margin-bottom: 0.1in; line-height: 120% } code.cjk { font-family: "Droid Sans Fallback" ...