两个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);
}
}

原文地址:https://www.cnblogs.com/lanliying/p/6046355.html