近日在开发中遇到了需要将一个list和另一个list重复的部分删除掉,嗯,本身是很简单的代码,只要用list自带的方法removeall就可以了,我的代码是这样的
public static void main(String[] args) { List<User> list1 = new ArrayList<>(); List<User> list2 = new ArrayList<>(); User user1 = new User("peipei", "123", "123.@163.com"); User user2 = new User("peipei", "123", "123.@163.com"); list1.add(user1); list2.add(user2); list1.removeAll(list2); System.out.println(list1); } static class User { String userName; String password; String email; public User(String userName, String password, String email) { super(); this.userName = userName; this.password = password; this.email = email; } /** * @return the userName */ public String getUserName() { return userName; } /** * @param userName the userName to set */ public void setUserName(String userName) { this.userName = userName; } /** * @return the password */ public String getPassword() { return password; } /** * @param password the password to set */ public void setPassword(String password) { this.password = password; } /** * @return the email */ public String getEmail() { return email; } /** * @param email the email to set */ public void setEmail(String email) { this.email = email; } /* (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { return "User [userName=" + userName + ", password=" + password + ", email=" + email + "]"; } }输出结果为:[User [userName=peipei, password=123, email=123.@163.com]]Emmm…嗯?并没有移除掉list1和list2相同的部分,排除掉打错字的情况下,看了一下removeall方法的源代码发现是用equals来判断list中的元素是否相等的,ok,看到这里其实已经想明白了,我们经常用equals来判断字符串的内容是否相等,这是因为在String类中把Object类中的equals方法进行了重写,我们在用equals来判断对象是否相等是就等同于,对象1 == 对象2,比较的是两个对象的地址是否相等,把 User user2 = new User(“peipei”, “123”, “123.@163.com”);这行代码改为User user2 = user1;其他不变,打印结果为 [] ,那说回来我要怎样来判断两个对象的值是否相等呢,那就和String类一样来重写equals方法,修改后的User类代码如下:
public static void main(String[] args) { List<User> list1 = new ArrayList<>(); List<User> list2 = new ArrayList<>(); User user1 = new User("peipei", "123", "123.@163.com"); User user2 = new User("peipei", "123", "123.@163.com"); list1.add(user1); list2.add(user2); list1.removeAll(list2); System.out.println(list1); } static class User { String userName; String password; String email; public User(String userName, String password, String email) { super(); this.userName = userName; this.password = password; this.email = email; } /** * @return the userName */ public String getUserName() { return userName; } /** * @param userName the userName to set */ public void setUserName(String userName) { this.userName = userName; } /** * @return the password */ public String getPassword() { return password; } /** * @param password the password to set */ public void setPassword(String password) { this.password = password; } /** * @return the email */ public String getEmail() { return email; } /** * @param email the email to set */ public void setEmail(String email) { this.email = email; } @Override public String toString() { return "User [userName=" + userName + ", password=" + password + ", email=" + email + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((email == null) ? 0 : email.hashCode()); result = prime * result + ((password == null) ? 0 : password.hashCode()); result = prime * result + ((userName == null) ? 0 : userName.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; User other = (User) obj; if (email == null) { if (other.email != null) return false; } else if (!email.equals(other.email)) return false; if (password == null) { if (other.password != null) return false; } else if (!password.equals(other.password)) return false; if (userName == null) { if (other.userName != null) return false; } else if (!userName.equals(other.userName)) return false; return true; } }再次运行测试结果为 [] 搞定 注:在重写equals方法时,必须重写hashcode方法,一个是为了效率,一个是为了防止出现两个对象equals为ture但是hashcode不同这样违背定理的事情出现。