实现一个java代码比较两个list集合的差异的工具类,list中存储的不是基本类型数据,如果list中包含list那么进行递归比较,并返回差异的属性
时间: 2024-01-22 10:20:44 浏览: 101
可以使用Java的反射机制,递归比较两个对象的属性值是否相等,如果不相等,则记录下差异属性的名称和值,最终返回所有的差异属性。
以下是一个示例代码,可以作为工具类使用:
```java
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ListComparator {
/**
* 比较两个list集合的差异
* @param list1 第一个list集合
* @param list2 第二个list集合
* @param <T> list中存储的数据类型
* @return 返回差异属性的名称和值
* @throws IllegalAccessException
*/
public static <T> Map<String, Object> compare(List<T> list1, List<T> list2) throws IllegalAccessException {
Map<String, Object> result = new HashMap<>();
int size1 = list1.size();
int size2 = list2.size();
// 如果两个list的大小不同,则它们肯定不相等
if (size1 != size2) {
result.put("size", size2 - size1);
return result;
}
// 遍历list1和list2,比较每个元素的属性值是否相等
for (int i = 0; i < size1; i++) {
T obj1 = list1.get(i);
T obj2 = list2.get(i);
Map<String, Object> diff = compare(obj1, obj2);
if (!diff.isEmpty()) {
result.put("index" + i, diff);
}
}
return result;
}
/**
* 比较两个对象的属性值是否相等
* @param obj1 第一个对象
* @param obj2 第二个对象
* @return 返回差异属性的名称和值
* @throws IllegalAccessException
*/
public static Map<String, Object> compare(Object obj1, Object obj2) throws IllegalAccessException {
Map<String, Object> result = new HashMap<>();
Class clazz = obj1.getClass();
// 获取类中所有的属性
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
Object val1 = field.get(obj1);
Object val2 = field.get(obj2);
if (val1 instanceof List && val2 instanceof List) {
// 如果属性是list类型,则递归比较两个list的差异
Map<String, Object> diff = compare((List)val1, (List)val2);
if (!diff.isEmpty()) {
result.put(field.getName(), diff);
}
} else if (!val1.equals(val2)) {
// 如果属性值不相等,则记录下差异属性的名称和值
result.put(field.getName(), val2);
}
}
return result;
}
}
```
使用方法:
```java
public class Test {
public static void main(String[] args) throws IllegalAccessException {
List<User> list1 = new ArrayList<>();
List<User> list2 = new ArrayList<>();
list1.add(new User("Tom", 18));
list1.add(new User("Jerry", 20));
List<Address> addresses1 = new ArrayList<>();
addresses1.add(new Address("China", "Beijing"));
addresses1.add(new Address("USA", "New York"));
list1.add(new User("Jack", 30, addresses1));
list2.add(new User("Tom", 18));
list2.add(new User("Jerry", 20));
List<Address> addresses2 = new ArrayList<>();
addresses2.add(new Address("China", "Shanghai"));
addresses2.add(new Address("USA", "New York"));
list2.add(new User("Jack", 31, addresses2));
Map<String, Object> result = ListComparator.compare(list1, list2);
System.out.println(result);
}
}
class User {
private String name;
private int age;
private List<Address> addresses;
public User(String name, int age) {
this.name = name;
this.age = age;
}
public User(String name, int age, List<Address> addresses) {
this.name = name;
this.age = age;
this.addresses = addresses;
}
// getter和setter方法省略
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", addresses=" + addresses +
'}';
}
}
class Address {
private String country;
private String city;
public Address(String country, String city) {
this.country = country;
this.city = city;
}
// getter和setter方法省略
@Override
public String toString() {
return "Address{" +
"country='" + country + '\'' +
", city='" + city + '\'' +
'}';
}
}
```
输出结果:
```
{index2={age=31, addresses={index0={city=Shanghai}}}}
```
表示第三个元素的age属性和addresses属性中第一个元素的city属性不相等。
阅读全文