HashSet实现原理
/*
HashSet的实现原理:
往HashSet添加元素的时候,HashSet会先调用元素的hashCode方法得到元素的哈希值 ,
然后通过元素 的哈希值经过移位等运算,就可以算出该元素在哈希表中 的存储位置。
情况1: 如果算出元素存储的位置目前没有任何元素存储,那么该元素可以直接存储到该位置上。
情况2: 如果算出该元素的存储位置目前已经存在有其他的元素了,那么会调用该元素的equals方法与该位置的元素再比较一次
,如果equals返回的是true,那么该元素与这个位置上的元素就视为重复元素,不允许添加,如果equals方法返回的是false,那么该元素运行添加。
*/
import java.util.*;
class Person{
String name;
int id;
public Person(String name, int id) {
this.name = name;
this.id = id;
}
@Override
public String toString() {
return "Person [name=" + name + ", id=" + id + "]";
}
@Override
public int hashCode() { //此时hashCode方法被调用4次
System.out.println("hashCode==============");
return this.id;
}
@Override
public boolean equals(Object obj) { ////此时equals方法被调用1次
System.out.println("equals------------");
Person p = (Person) obj;
return this.id == p.id;
}
}
public class Demo5 {
public static void main(String[] args) {
HashSet set = new HashSet();
set.add(new Person("大师兄", 1));
set.add(new Person("二师兄", 3));
set.add(new Person("沙师弟", 2));
//id唯一性,若id相同,就应该为同一人,为此,重写hashCode方法和equals方法
System.out.println("添加成功吗?" + set.add(new Person("师傅", 1)));
Iterator it = set.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
结果:
hashCode==============
hashCode==============
hashCode==============
hashCode==============
equals------------
添加成功吗?false
Person [name=大师兄, id=1]
Person [name=沙师弟, id=2]
Person [name=二师兄, id=3]
注意,这个是无序、不可重复的
比如:
HashSet set = new HashSet();
set.add(new Person("大师兄", 1));
set.add(new Person("二师兄", 3));
set.add(new Person("沙师弟", 2)); set.add(new Person("大师兄", 43));
set.add(new Person("二师兄", 333));
set.add(new Person("沙师弟", 22));
set.add(new Person("大师兄", 33));
set.add(new Person("二师兄", 344));
set.add(new Person("沙师弟", 211));
此时结果:
hashCode==============
hashCode==============
hashCode==============
hashCode==============
hashCode==============
hashCode==============
hashCode==============
hashCode==============
hashCode==============
hashCode==============
equals------------
添加成功吗?false
Person [name=大师兄, id=1]
Person [name=大师兄, id=33]
Person [name=沙师弟, id=2]
Person [name=二师兄, id=3]
Person [name=沙师弟, id=211]
Person [name=沙师弟, id=22]
Person [name=二师兄, id=344]
Person [name=大师兄, id=43]
Person [name=二师兄, id=333]
HashSet实现原理的更多相关文章
- Java集合 HashSet的原理及常用方法
目录 一. HashSet概述 二. HashSet构造 三. add方法 四. remove方法 五. 遍历 六. 合计合计 先看一下LinkedHashSet 在看一下TreeSet 七. 总结 ...
- Java集合之HashSet/TreeSet原理
Set集合 1.HashSet 只去重复, 没有顺序 HashSet的add方法会调用hashCode和equals, 所以存储在HashSet中的对象需要重写这两个方法. 2.TreeSet ...
- List、Set集合系列之剖析HashSet存储原理(HashMap底层)
目录 List接口 1.1 List接口介绍 1.2 List接口中常用方法 List的子类 2.1 ArrayList集合 2.2 LinkedList集合 Set接口 3.1 Set接口介绍 Se ...
- Java set接口之HashSet集合原理讲解
Set接口 java.util.set接口继承自Collection接口,它与Collection接口中的方法基本一致, 并没有对 Collection接口进行功能上的扩充,只是比collection ...
- 红黑树规则,TreeSet原理,HashSet特点,什么是哈希值,HashSet底层原理,Map集合特点,Map集合遍历方法
==学习目标== 1.能够了解红黑树 2.能够掌握HashSet集合的特点以及使用(特点以及使用,哈希表数据结构) 3.能够掌握Map集合的特点以及使用(特点,常见方法,Map集合的遍历) 4.能够掌 ...
- Java HashSet工作原理及实现
1. 概述 This class implements the Set interface, backed by a hash table (actually a HashMap instance). ...
- HashSet实现原理及源码分析
HashSet简介 HashSet是Set接口实现,它按照Hash算法来存储集合中的元素 不保证元素顺序 HashSet是非同步的,如果多个线程同时访问一个HashSet,要通过代码来保证其同步 集合 ...
- Java面试题 从源码角度分析HashSet实现原理?
面试官:请问HashSet有哪些特点? 应聘者:HashSet实现自set接口,set集合中元素无序且不能重复: 面试官:那么HashSet 如何保证元素不重复? 应聘者:因为HashSet底层是基于 ...
- 3.Java集合-HashSet实现原理及源码分析
一.HashSet概述: HashSet实现Set接口,由哈希表(实际上是一个HashMap实例)支持,它不保证set的迭代顺序很久不变.此类允许使用null元素 二.HashSet的实现: 对于Ha ...
随机推荐
- 【Unity编程】四元数(Quaternion)与欧拉角
版权声明:本文为博主原创文章,欢迎转载.请保留博主链接:http://blog.csdn.net/andrewfan 欧拉旋转.四元数.矩阵旋转之间的差异 除了欧拉旋转以外,还有两种表示旋转的方式:矩 ...
- Centos7配置文件共享服务器SAMBA三步曲(转)
1.安装 yum install samba samba-client samba-common -y 2.配置 备份已有配置 mv /etc/samba/smb.conf /etc/samba/sm ...
- windows和linux删除文件方法
Windows下bat文件内容如下:复制代码 代码如下: @echo offforfiles -p "D:\servers\apache2.2\logs" -s -m *.log ...
- [Android]使用RecyclerView替代ListView(四:SeizeRecyclerView)
以下内容为原创,欢迎转载,转载请注明 来自天天博客:<> [Android]使用RecyclerView替代ListView(四:SeizeRecyclerView) 在RecyclerV ...
- *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ViewController > setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ViewController > ...
- 用代码控制退出APP
+ (void)exitApplication { AppDelegate *app = [UIApplication sharedApplication].delegate; UIWindow *w ...
- nginx 入门配置
这个星期公司的定期分享内容是Nginx,于是就要写作业了. 一.动静分离 1.下载Windows 版本的Nginx,解压,放到C盘下.进入目录,然后按然shift键右键,打开命令行,输入: start ...
- EntityFramework Core高并发深挖详解,一纸长文,你准备好了吗?
前言 之前有关EF并发探讨过几次,但是呢,博主感觉还是有问题,为什么会觉得有问题,其实就是理解不够透彻罢了,于是在项目中都是用的存储过程或者SQL语句来实现,利用放假时间好好补补EF Core并发的问 ...
- Adobe 系列软件通用破解方式(animate cc,Photoshop cc,Flash cc)等
破解之前准备工作: ①:安装好 试用版的 Adobe软件 ②:下载好破解软件: amtemu.v0.9.2-painter,下载地址:链接:http://pan.baidu.com/s/1nvNR74 ...
- python修行:练习购物车
product_list = [ ('Iphone',5800), ('Mac Pro',9800), ('Bike',800), ('Watch',10600), ('Coffee',31), (' ...