两个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 ...
随机推荐
- strstr函数
原型:char * strstr( char *haystack, char *needle ) 用法:#include <string.h> 功能:在haystack中寻找needle ...
- SQL Server类型的对应关系
bit bool tinyint byte smallint short int int bigint long real float float double money decimal datet ...
- spring启动component-scan类扫描加载过程---源码分析
http://blog.csdn.net/xieyuooo/article/details/9089441#comments
- 3.springMVC+spring+Mybatis整合Demo(单表的增删该查,这里主要是贴代码,不多解释了)
前面给大家讲了整合的思路和整合的过程,在这里就不在提了,直接把springMVC+spring+Mybatis整合的实例代码(单表的增删改查)贴给大家: 首先是目录结构: 仔细看看这个目录结构:我不详 ...
- 通过dataGridView控件中的checkBox控件对数据库进行批量删除
string id_s = ""; ; i < dataGridView1.Rows.Count; i++) //遍历所有行 { if (dataGridView1.Rows ...
- HashMap的两种实现方式
本文主要简要分析了Java中和Redis中HashMap的实现,并且对比了两者的异同 1.Java的实现 下图表示了Java中一个HashMap的主要实现方式 因为大家对于Java中HashMap的实 ...
- 新博客地址: kuangbin.org
RT. 买了新域名,原来的kuangbin.net已经废了,数据已经移动到了kuangbin.org
- jquery blockui 遮罩
参考 : http://bookshadow.com/weblog/2014/09/26/jquery-blockui-js-introduction/ blockUI.html blockUI.ht ...
- C语言学习笔记(一)_hello world程序中涉及的c语言元素
#include <stdio.h> //使用printf函数之前必须include<stdio.h> int main() { int i; //声明一个变量 printf( ...
- transitionend的运用案例
transitionend事件代表着过渡动画结束后 原生的绑定方法 obj.addEventListener('transitionEnd', function(){ //do soming }) 我 ...