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;
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 ;
}
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;
}
}