一、通过查看add方法的源码,我们知道这个方法底层依赖 两个方法:hashCode()和equals()。如果类没有重写这两个方法,默认使用的Object()。一般来说仅仅hashCode可能都不会相同,equals也不相同;则会全部添加到集合中去。
* 例如自定义对象没有重写hashCode()和equals()方法的时候,就全部打印出来。这也是不重复的原因。 * 而String类(写字符串对象的时候)重写了hashCode()和equals()方法,所以,它就可以把内容相同的字符串去掉。实体类没有hashCode()和equals()方法的时候
public class Student {
private String name;
private String password; public Student() { super(); // TODO Auto-generated constructor stub }public Student(String name, String password) {
super(); this.name = name; this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } /* public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((password == null) ? 0 : password.hashCode()); return result; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (password == null) { if (other.password != null) return false; } else if (!password.equals(other.password)) return false; return true; }*/ public static void main(String[] args) { Set<Student> set = new HashSet<Student>(); set.add(new Student("yj","123")); set.add(new Student("yj","123")); set.add(new Student("yc","123")); set.add(new Student("yc","123")); for (Student ob : set) { System.out.println(ob.getName()+"---"+ob.getPassword()); } } }实体类没有hashCode()和equals()方法的时候运行打印的结果
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
实体类有hashCode()和equals()方法的时候
package it.fuxi;
import java.util.HashSet;
import java.util.Set;public class Student {
private String name;
private String password; public Student() { super(); // TODO Auto-generated constructor stub }public Student(String name, String password) {
super(); this.name = name; this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((password == null) ? 0 : password.hashCode()); return result; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (password == null) { if (other.password != null) return false; } else if (!password.equals(other.password)) return false; return true; } public static void main(String[] args) { Set<Student> set = new HashSet<Student>(); set.add(new Student("yj","123")); set.add(new Student("yj","123")); set.add(new Student("yc","123")); set.add(new Student("yc","123")); for (Student ob : set) { System.out.println(ob.getName()+"---"+ob.getPassword()); } } }实体类有hashCode()和equals()方法的时候运行打印的结果