【定义】

有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. Oracle保留两位小数的函数

    1.最终保存成字符串类型 使用to_char()函数 // 其9代表:如果存在数字则显示数字,不存在则显示空格 // 其0代表:如果存在数字则显示数字,不存在则显示0,即占位符 // 其FM代表:删除 ...

  2. 开发webapp手机返回键 退出问题 摘录

    mui进行手机物理键的监听 确保引入mui 调用以下函数 // android 返回按键处理 androidBack(store, data) { try { mui.init({ keyEventB ...

  3. Error: Apache shutdown unexpectedly --解决

    原因1:端口占用,百度可解决 原因2: 配置的启动目录不存在~~~ 解决 修改默认目录:

  4. javascript:控制一个元素高度始终等于浏览器高度

    window.onresize = function(){ this.opHtight()} //给浏览器添加窗口大小改变事件window.onresize = function(){ this.op ...

  5. 【BUG记录】记一次游戏越来越卡的BUG

    U3D的MOBA项目,测试过程中,10分钟以后,游戏帧率开始缓慢下降,约3-5分钟后,由60帧下降到小于10帧,编辑器模式. 打开profiler,看到CPU占用非常高,每帧都有24K的GC, 时间占 ...

  6. Kotlin语言编程技巧集

    空语句 Kotlin 语言中的空语句有 {} Unit when (x) { 1 -> ... 2 -> ... else -> {} // else -> Unit } Wh ...

  7. python实现查找算法:二分查找法

    二分查找算法也称折半查找,基本思想就是折半,和平时猜数字游戏一样,比如猜的数字时67,猜测范围是0-100,则会先猜测中间值50,结果小了,所以就会从50-100猜测,中间值为75,结果大了,又从50 ...

  8. delphi控制本计算机和远程计算机关机等

    unit mainunit; {远程关机源码} interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Con ...

  9. Structs复习 包含外部xml和默认Actiion

    包含外部xml 可以用 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PU ...

  10. Java学习05 (第一遍) - JSP与Servlet

    JSP 客户端发出Request请求,JSP容器将JSP转译为Servlet的源码,再编译加载到内存执行,结果Response到客户端. Request->JSP->Servlet(jav ...