HashSet元素不重复底层比较
来源:互联网 发布:游族网络 大皇帝 手游 编辑:程序博客网 时间:2024/06/05 09:23
Set集合特点:
1.无序
2.不允许元素重复
HashSet
特点:提高查询速度。
HashSet中如何判断两个对象是否相同?
两个充分必要条件:
- equals()的值为true
- hashCode()返回值相等。hashCode()的值决定了该对象在该集合中的位置。
由于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; }}
阅读全文
1 0
- HashSet元素不重复底层比较
- HashSet插入不重复元素
- 利用 HashSet不重复特性返回素组中重复元素
- HashSet删除重复元素
- HashSet不重复存储元素的执行过程
- HashSet中的元素比较详解
- 更加高效的去除数组中重复元素的算法(不使用HashSet)
- 补充HashSet集合hashCode和equals的重写,保证元素的不重复
- HashSet类是如何实现添加元素保证不重复的---哈希码的原理
- Java记录 -56- 详解HashSet不含重复元素的实现
- 去除数组中的重复元素---hashset
- HashSet、TreeSet 去除Class重复的元素
- ArrayList和HashSet如何去除重复元素
- HashSet保存自定义不重复对象
- hashSet保证不重复的原理
- List不添加重复元素
- 去除数组中的重复元素(引用类库hashset)
- 怎么删除一个数组中的重复元素(HashSet)
- Unity中实时检测当前手机网络,网络切换时给用户一个提示
- java网络编程
- super关键字,super关键字和this关键字的区别
- HDU
- eclipse 下导入外部jar包的3种方式
- HashSet元素不重复底层比较
- Python绘制图标基础
- 面试---算法排序基础(1)
- JDBC之使用Spring提供的JdbcTemplate进行数据库操作
- B1021
- 棋盘问题***
- NYOJ 6 喷水装置(一)
- 邻接表原理详解
- 正则表达式 && sed应用