【定义】

有index的集合

【hash的原理】

term for a situation when two different objects return the same hashcode: hash collision

就是无规律的一一对应排序,相同object对应的HASH应该相同,相同对应的HASH应该不同。具体实现:hashCode(It returns an int hash-code depending on the memory address of the object 根据obeject的位置来对应in the heap) equal() 都是自动继承的。

Use a function on hashcode, such as modulo, to calculate the index into the hashtable and then store the object at that index 根据hash的index来查找物体

【实现】

object有三种方法toString equal hashcode,通过传入memory address来计算值。同一obeject的不同state状态:要避免collision。

public int hashCode() {
return Objects.hash(title, author, year); //hash on instance variable values
}
//equals() implements equivalence relation - reflexive, symmetric, transitive, consistent –
// on non-null object references
@Override
public boolean equals(Object o) {
if (o == null) return false; // if o is null, then it can’t be equal to ‘this’
if (this == o) return true; // if both point to same object, then they are equal
if (getClass() != o.getClass()) return false; //if not of same type, they can’t be equal
Book b = (Book) o; //now that we know o is unique, non-null Book object, cast it to Book
return title.equals(b.title) && author.equals(b.author) && (year == b.year); //compare values
}

【步骤】

先用hashcode,再用equals。因为state不同要overload, 只overload一个会发生inconsistent。

【区别】

用object/key来hash

【treemap】

排序的map,里面是BST

【map的三个集合】

entryset keyset values

remove(k) 是remove key = k的entry

Hashed collections哈希集合的更多相关文章

  1. 哈希集合——hashSet

    /**     哈希集合特点:存取顺序不确定,同一个哈希值的位置可以存放多个元素,                   哈希集合存放元素的时候是先判断哈希地址值:hashCode()是否相同,如果不同 ...

  2. [Swift]LeetCode705. 设计哈希集合 | Design HashSet

    Design a HashSet without using any built-in hash table libraries. To be specific, your design should ...

  3. [TimLinux] Python __hash__ 可哈希集合

    规则: __hash__ 应该返回一个整数,hash()函数计算基础类型的hash值 可哈希集合:set(), forzenset(), dict() 三种数据结构操作要求 key 值唯一,判断唯一的 ...

  4. LeetCode 705:设计哈希集合 Design HashSet

    题目: 不使用任何内建的哈希表库设计一个哈希集合 具体地说,你的设计应该包含以下的功能 add(value):向哈希集合中插入一个值. contains(value) :返回哈希集合中是否存在这个值. ...

  5. C#泛型集合之——哈希集合

    1.特点:HashSet 中元素不重复,容量为元素个数,自动增大.是一组值,是高性能的数学集合. 2.创建: (1)HashSet<类型> 集合名 = new HashSet<类型& ...

  6. Leetcode705.Design HashSet设置哈希集合

    不使用任何内建的哈希表库设计一个哈希集合 具体地说,你的设计应该包含以下的功能 add(value):向哈希集合中插入一个值. contains(value) :返回哈希集合中是否存在这个值. rem ...

  7. leetcode_二叉树验证(BFS、哈希集合)

    题目描述: 二叉树上有 n 个节点,按从 0 到 n - 1 编号,其中节点 i 的两个子节点分别是 leftChild[i] 和 rightChild[i]. 只有 所有 节点能够形成且 只 形成 ...

  8. Java实现 LeetCode 705 设计哈希集合(使用数组保存有没有被用过)

    705. 设计哈希集合 不使用任何内建的哈希表库设计一个哈希集合 具体地说,你的设计应该包含以下的功能 add(value):向哈希集合中插入一个值. contains(value) :返回哈希集合中 ...

  9. C#LeetCode刷题之#705-设计哈希集合​​​​​​​(Design HashSet)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4114 访问. 不使用任何内建的哈希表库设计一个哈希集合 具体地说 ...

随机推荐

  1. Win2008R2配置WebDeploy(转)

    一.配置服务器 1.安装管理服务 2.点击管理服务进行配置 3.安装WebDeploy 3.1通过离线安装包方式安装: https://www.iis.net/downloads/microsoft/ ...

  2. Oracle 学习总结 - 内存优化

    实例内存优化 开启自动内存管理 1. 设置memory_max_target alter system set memory_max_target=1G scope=spfile; /*init.or ...

  3. css:调整placeholder样式

    input::-webkit-input-placeholder, textarea::-webkit-input-placeholder { color: #C5CACF; } input:-moz ...

  4. Oracle问题小结

    1.win8.1安装Oracle11g后,重启电脑,出现黑屏. 解决办法:安全模式下,找到以oracle开头的全部服务,所有“自动”或者“自动(延迟启动)”的都设置为“手动”,只需要开启OracleO ...

  5. 高质量C++/C编程指南

    http://man.chinaunix.net/develop/c&c++/c/c.htm#_Toc520634042 高质量C++/C编程指南 文件状态 [  ] 草稿文件 [√] 正式文 ...

  6. impdp导入文件失败问题解决(ORA-39001/ORA-39000/ORA-39143)

    测试环境 SuSE11 + ORACLE11gR2 问题现象 执行 impdp导入现场导回的dmp文件,导入失败.错误提示如下 $impdp sysdb/oracle directory=imp_da ...

  7. ps记录

    图层 ctrl+j:通过拷贝的图层 颜色填充 alt+delete:前景色填充(或alt+backspace) ctrl+delete:背景色填充(或ctrl+backspace) 多图层合并一 分组 ...

  8. pytest 单元测试

    pytest简介 pytest 是python的一种单元测试框架,它非常的简洁.清晰. pytest 安装 pip install -U pytest 查看pytest版本 pytest --vers ...

  9. html中,纯数字或纯英文的一串字符超出父容器不会折行显示,如何解决?

    这种情况在软件使用过程中一般不会出现,只有测试人员在测试的时候手比较贱会给你弄一个这种数据,当然这也算是bug吧. 如图:“经营范围”的值严重超出父容器长度,并且没有像“服务信息”一样折行显示.这种情 ...

  10. hbase高可用集群部署(cdh)

    一.概要 本文记录hbase高可用集群部署过程,在部署hbase之前需要事先部署好hadoop集群,因为hbase的数据需要存放在hdfs上,hadoop集群的部署后续会有一篇文章记录,本文假设had ...