Hashed collections哈希集合
【定义】
有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哈希集合的更多相关文章
- 哈希集合——hashSet
/** 哈希集合特点:存取顺序不确定,同一个哈希值的位置可以存放多个元素, 哈希集合存放元素的时候是先判断哈希地址值:hashCode()是否相同,如果不同 ...
- [Swift]LeetCode705. 设计哈希集合 | Design HashSet
Design a HashSet without using any built-in hash table libraries. To be specific, your design should ...
- [TimLinux] Python __hash__ 可哈希集合
规则: __hash__ 应该返回一个整数,hash()函数计算基础类型的hash值 可哈希集合:set(), forzenset(), dict() 三种数据结构操作要求 key 值唯一,判断唯一的 ...
- LeetCode 705:设计哈希集合 Design HashSet
题目: 不使用任何内建的哈希表库设计一个哈希集合 具体地说,你的设计应该包含以下的功能 add(value):向哈希集合中插入一个值. contains(value) :返回哈希集合中是否存在这个值. ...
- C#泛型集合之——哈希集合
1.特点:HashSet 中元素不重复,容量为元素个数,自动增大.是一组值,是高性能的数学集合. 2.创建: (1)HashSet<类型> 集合名 = new HashSet<类型& ...
- Leetcode705.Design HashSet设置哈希集合
不使用任何内建的哈希表库设计一个哈希集合 具体地说,你的设计应该包含以下的功能 add(value):向哈希集合中插入一个值. contains(value) :返回哈希集合中是否存在这个值. rem ...
- leetcode_二叉树验证(BFS、哈希集合)
题目描述: 二叉树上有 n 个节点,按从 0 到 n - 1 编号,其中节点 i 的两个子节点分别是 leftChild[i] 和 rightChild[i]. 只有 所有 节点能够形成且 只 形成 ...
- Java实现 LeetCode 705 设计哈希集合(使用数组保存有没有被用过)
705. 设计哈希集合 不使用任何内建的哈希表库设计一个哈希集合 具体地说,你的设计应该包含以下的功能 add(value):向哈希集合中插入一个值. contains(value) :返回哈希集合中 ...
- C#LeetCode刷题之#705-设计哈希集合(Design HashSet)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4114 访问. 不使用任何内建的哈希表库设计一个哈希集合 具体地说 ...
随机推荐
- flash推流工具<转>
https://github.com/young-cowboy/young-cowboy.github.io https://www.cnblogs.com/xiaoniuzai/p/7129036. ...
- python反汇编函数字节码
使用dis模块 >>> def test(): ... print(1) ... a = 1 ... print(a) ... >>> from dis impor ...
- ACM__菜鸟之经典错误
1:多组输入与单组输入 Input contains multiple test cases. Input contains a single test case. 2: EOF=-1 while( ...
- Linux下tomcat运行命令
tomcat启动 [root@master webapps]# /usr/local/tomcat7.0/bin/catalina.sh start startup.sh的源代码,其实就是执行 c ...
- 趣味编程:静夜思(Python版)
from itertools import groupby def verticalWriting(txt, offset): l = lambda x: x[0] % offset for (_, ...
- WebRequest + Https + 憑証錯誤 = 作業逾時
前言 一般的系統一登入時,都會顯示公告訊息,我們的系統也是! 因為系統效能問題,所以就幫忙看了程式.發現,每次登入時,都會到DB取公告資料,但公告並不會因為個人而有所不同,是針對整個系統的呀! 所以就 ...
- TLS协议工作过程;如何应用TLS/SSL协议为WEB流量提供安全
SSL/TLS协议的基本过程是这样的: 客户端向服务器端索要并验证公钥. 双方协商生成"对话密钥". 双方采用"对话密钥"进行加密通信. 上面过程的前两 ...
- Structs复习 Result第二部分
1.动态结果 2.web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app versio ...
- gdufe1534-小小怪一定认真听课-dfs
Problem Description: 又到了选课的时间啦.大一萌新小小怪下士第一次选课没有制定好高效的策略,导致第一学期的学分不高,他想在第二学期获得尽可能多的学分,因此作为小小怪下士的上司搭档兼 ...
- sudo su到root或到其它用户报这种错
一般不会出现这种情况,除非你进入的这个目录已经被删了.然后切换用户就找不到这个目录,所以报错. 这种情况下我们只要去到一个正常的目录就好了,比如:cd ~ 然后再切换,ok.