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. SQL日期格式,转自will哥

    我之前一直認為 SQL Server 針對日期處理的函數不夠多(如果跟 MySQL 比較),尤其是處理日期欄位轉字串的時候,常常因為要輸出特定的格式而懊惱不已,常常一不小心就寫了一長串,很不易閱讀. ...

  2. java:关于文件下载

    开发中遇到的问题 一. 场景描述:用户先指定下载路径,服务器将资源文件下载到指定的路径. 首先:该系统采用的是B/S架构,即浏览器.服务器(.数据库).    单纯通过B/S架构,就想要在服务器中去操 ...

  3. 快速解析超大XML不占用太大内存

    import xml.etree.ElementTree as ET def parse_res(xml_file): res_dic = {} tmp_lst_lev1 = [] tmp_lst_l ...

  4. 通过pinyin4j.jar将(汉字拼音混合字符串)转化成字母首字母

    通过pinyin4j.jar将(汉字拼音混合字符串)转化成字母首字母 例如 我的中国心    ==> wdzgx 我的中国心ya ==> wdzgxya woai我的中国 ==> w ...

  5. Activity之间传递参数(二)

    ------siwuxie095 传递数据包 1.传递数据包要用到Bundle,MainActivity.java中: package com.siwuxie095.sendargs; import ...

  6. CSS3 animation 的尝试

    下面是动画效果: .zoombie { width: 55px; height: 85px; background-image: url("http://images2015.cnblogs ...

  7. SAP 采购订单行项目中科目分配被隐藏,发现行项目设置中显示字段长度为0

    1.sm30 维护 视图 TCVIEW 修改对应字段的显示长度

  8. android中的requestFocus标签

    <requestFocus />标签用于指定屏幕中的焦点View 用法:置于Views标签内部 ex: <EditText             android:id=" ...

  9. 支付宝App支付签名和验签

    代码: using CMS.Utility.ReturnResult; using OAuthWebAPI.Package; using Common; using System; using Sys ...

  10. ABAP 字符串操作

    1).SHIFT:截断字符串 SHIFT {c} [BY {n} PLACES] [{mode}].: 作用:去掉字符串的前n个位置的字符,如果n未指定,默认为1,如果指定的n小于等于0,则字符串不变 ...