概述
import org.junit.Test;
import java.lang.reflect.Field;
import java.util.*;
public class TestArrayList {
@Test
public void testOverrideEquals() {
List<User> list = new ArrayList<User>();
User u1 = new User("3", "小强", "4");
User u2 = new User("4", "小强", 21);
User u3 = new User("4", "小强1", 21);
User u4 = new User("4", "小强1", "2");
list.add(u1);
list.add(u2);
list.add(u3);
list.add(u4);
Map<User, List<User>> map = new HashMap<>();
for (User user : list) {
List<User> listmap = map.get(user);
if (listmap == null) {
listmap = new ArrayList<>();
}
listmap.add(user);
map.put(user, listmap);
}
System.out.println(map);
ArrayList<Object> newLists = new ArrayList<>();
for (Map.Entry<User, List<User>> m : map.entrySet()) {
List<User> users = m.getValue();
User temp = new User();
for (User user : users) {
Class<? extends User> userClass = user.getClass();
Class<? extends User> tempClass = temp.getClass();
Field[] fields = userClass.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
try {
Object value = field.get(user);
boolean result = isResult(value);
if (result) {
Field tempClassField = tempClass.getDeclaredField(field.getName());
tempClassField.setAccessible(true);
tempClassField.set(temp, value);
}
} catch (IllegalAccessException | NoSuchFieldException e) {
e.printStackTrace();
}
}
}
newLists.add(temp);
}
System.out.println(newLists);
}
private boolean isResult(Object value) {
boolean result = false;
if (value != null) {
if (value instanceof Integer) {
if (Integer.parseInt(value.toString()) != 0) {
result = true;
}
} else {
result = true;
}
}
return result;
}
}
实体类
import org.apache.commons.lang3.StringUtils;
/**
* .
*
* @Author Chenglin Zhu
* @Date 2021/4/15 20:49
*/
public class User {
private String id;
private String name;
private int age;
private String nation;
public User(){
}
public User(String id, String name, int age, String nation) {
this.id = id;
this.name = name;
this.age = age;
this.nation = nation;
}
public User(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public User(String id, String name, String nation) {
this.id = id;
this.name = name;
this.nation = nation;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNation() {
return nation;
}
public void setNation(String nation) {
this.nation = nation;
}
@Override
public String toString() {
return "User{" +
"id='" + id + ''' +
", name='" + name + ''' +
", age=" + age +
", nation='" + nation + ''' +
'}';
}
@Override
public boolean equals(Object obj) {
if(this == obj){
return true;//地址相等
}
if(obj == null){
return false;//非空性:对于任意非空引用x,x.equals(null)应该返回false。
}
if(obj instanceof User){
User other = (User) obj;
//需要比较的字段相等,则这两个对象相等
if(equalsStr(this.name, other.name)){
return true;
}
}
return false;
}
private boolean equalsStr(String str1, String str2){
if(StringUtils.isEmpty(str1) && StringUtils.isEmpty(str2)){
return true;
}
if(!StringUtils.isEmpty(str1) && str1.equals(str2)){
return true;
}
return false;
}
@Override
public int hashCode() {
int result = 17;
result = 31 * result + (name == null ? 0 : name.hashCode());
return result;
}
}
最后
以上就是健壮汉堡为你收集整理的属性相同的集合对象合并成一个实体类的全部内容,希望文章能够帮你解决属性相同的集合对象合并成一个实体类所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复