package HashSet;
import java.util.HashSet;
/*
* HashSet是存放不重复的元素的集合,是无序的
* 如何判断元素是否是重复?
* 基本数据类型:内容
* 引用数据类型:堆地址
通常是用HashCode和equals方法进行比较
如果hashcode相等,equals 可能不等
如果hashcode不等,equals一定不等,
如果equals相等,hashcode一定相等
如果equals不等 ,hashcode可能相等
* 注:java默认的是不等,比如在创建对象时,两个对象的属性是一样的,但是堆中地址是不一样的,所以两个对象是不等的,但如果重写了
* hashCode和equals方法,两者就相等。但是如果仅仅重写了equals方法,得出的结果是相等的,但hashcode是不等的,所以这就是
* hashcode不等,equals相等,这时就出错了,明明是不等的对象,就会误认为是相等的对象,所以在重写是,必须要将hashcode和equals
* 两个方法都重写。如果只重写hascode方法,会得到相同的hashcode,但是equals是不等的。
* */
public class HashSet_1 {
public static void main(String[] args) {
HashSet<Person> h=new HashSet<Person>();
/*h.add("123");
h.add("abc");
h.add("hello");
h.add("123");
System.out.println(h.size());
for(Object o:h) {
System.out.println(o);
}*/
Person p1=new Person(1,"test");
Person p2=new Person(1,"test");
h.add(p1);
h.add(p2);
System.out.println(h.size());
System.out.println(p1.hashCode());
System.out.println(p2.hashCode());
System.out.println(p1.equals(p2));
}
}
class Person{
private int id;
private String name;
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(int id, String name) {
super();
this.id = id;
this.name = name;
}
@Override
public int hashCode() {
System.out.println("hashcode方法");
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
/*@Override
public boolean equals(Object obj) {
System.out.println("equals方法");
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}*/
}
import java.util.HashSet;
/*
* HashSet是存放不重复的元素的集合,是无序的
* 如何判断元素是否是重复?
* 基本数据类型:内容
* 引用数据类型:堆地址
通常是用HashCode和equals方法进行比较
如果hashcode相等,equals 可能不等
如果hashcode不等,equals一定不等,
如果equals相等,hashcode一定相等
如果equals不等 ,hashcode可能相等
* 注:java默认的是不等,比如在创建对象时,两个对象的属性是一样的,但是堆中地址是不一样的,所以两个对象是不等的,但如果重写了
* hashCode和equals方法,两者就相等。但是如果仅仅重写了equals方法,得出的结果是相等的,但hashcode是不等的,所以这就是
* hashcode不等,equals相等,这时就出错了,明明是不等的对象,就会误认为是相等的对象,所以在重写是,必须要将hashcode和equals
* 两个方法都重写。如果只重写hascode方法,会得到相同的hashcode,但是equals是不等的。
* */
public class HashSet_1 {
public static void main(String[] args) {
HashSet<Person> h=new HashSet<Person>();
/*h.add("123");
h.add("abc");
h.add("hello");
h.add("123");
System.out.println(h.size());
for(Object o:h) {
System.out.println(o);
}*/
Person p1=new Person(1,"test");
Person p2=new Person(1,"test");
h.add(p1);
h.add(p2);
System.out.println(h.size());
System.out.println(p1.hashCode());
System.out.println(p2.hashCode());
System.out.println(p1.equals(p2));
}
}
class Person{
private int id;
private String name;
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(int id, String name) {
super();
this.id = id;
this.name = name;
}
@Override
public int hashCode() {
System.out.println("hashcode方法");
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
/*@Override
public boolean equals(Object obj) {
System.out.println("equals方法");
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}*/
}