HashSet元素不重复底层比较

时间:2022-08-19 16:47:54

Set集合特点:
1.无序
2.不允许元素重复

HashSet元素不重复底层比较

HashSet

特点:提高查询速度。

HashSet中如何判断两个对象是否相同?

两个充分必要条件:

  • equals()的值为true
  • hashCode()返回值相等。hashCode()的值决定了该对象在该集合中的位置。
    HashSet元素不重复底层比较

由于hashCode()值相同equals()返回false的情况比较麻烦,所以存储在哈希表中的对象都应该覆盖equals方法和hashCode()方法,并且保证equals()返回true的时候,hashCode()值也相等(只存在这种情况)。

如果需要把我们自定义的对象存储到哈希表中,该类的对象应该覆盖equals()和hashCode()方法,并在该方法中提供自己的判断规则。
如下例:


import java.util.HashSet;
import java.util.Set;

public class HashSetDemo {

public static void main(String[] args) {
Set<Student> set=new HashSet<>();
set.add(new Student(1,"Tom",17));
set.add(new Student(2,"Will",18));
set.add(new Student(1,"Tom",17));
set.add(new Student(1,"Will",18));
//在不重写equals()方法时,用的Object的equals,只比较内存地址。
System.out.println(set.size());// 4
//所以打印出set里面是4个不同的对象
}

}
class Student{
int id;
String name;
int age;
public Student(int id, String name, int age) {

this.id = id;
this.name = name;
this.age = age;
}
@Override//覆盖Object里面的toString()
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
package day11;

import java.util.HashSet;
import java.util.Set;

public class HashSetDemo {

public static void main(String[] args) {
Set<Student> set=new HashSet<>();


set.add(new Student(1,"Tom",17));
set.add(new Student(2,"Will",18));
set.add(new Student(1,"Tom",17));
set.add(new Student(1,"Jery",18));
//在不重写equals()方法时,用的Object的equals,只比较内存地址。
System.out.println(set.size());// 4
//所以打印出set里面是4个不同的对象
}

}
class Student{
int id;
String name;
int age;
public Student(int id, String name, int age) {

this.id = id;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
}

/**
* Alt+Shift+s:自动导入hashCode()和equals()
* 根据学号区分是否为一个对象
* size=2
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
}
@Override

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 (id != other.id)
return false;
return true;
}
**/


/**
*Alt+Shift+s:自动导入hashCode()和equals()
* 根据学号,姓名区分是否为一个对象
* size=3
@Override

public int 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) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) 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;
}

*/

//Alt+Shift+s:自动导入hashCode()和equals()
// * 根据学号,姓名,年龄区分是否为一个对象
// * size=4
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.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;
Student other = (Student) obj;
if (age != other.age)
return false;
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;
}
}