【定义】

有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. APP-8-文本语音

    1.百度语音合成JS文件 baidu_tts_cors.js /** * 浏览器调用语音合成接口 * @param {Object} param 百度语音合成接口参数 * 请参考 https://ai ...

  2. Leetcode 题解 First Missing Positive

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  3. self, super理解

    self是方法参数列表中的第一个参数,是运行时决定的. super是编译器符号,是编译时决定的.super的含义为从父类开始寻找相应的方法,父类在编译的时候就已经决定了. 一个关键点:super并不代 ...

  4. 知识点6: v-for列表循环

    效果图: 只手写了第一个dom: 剩下4个dom由v-for生成. html <div class="item" id="item"> <di ...

  5. javaweb 学习系列【转】

    http://www.cnblogs.com/xdp-gacl/category/574705.html jsp指令 http://www.cnblogs.com/huiyuantang/p/5332 ...

  6. JAVAWEB 一一ibatis(框架)

    ,升级版是mybatis,在配置文件里写sql语句对字段进行CURD) jar包 sqlMapConfig <?xml version="1.0" encoding=&quo ...

  7. Hibernate学习笔记2.3(Hibernate基础配置)

    映射,注释可以放在成员变量上面,也可以放在get方法上面 写在成员变量的话 破坏了java的面向对象思维 直接让hibernate访问内部的私有元素 要是能直接设指不合适哈哈 所以主张写在get方法上 ...

  8. C# Winform添加全局快捷键(老板键)

    using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.W ...

  9. SPSS-判别分析

    判别分析 判别分析是一种有效的对个案进行分类分析的方法.和聚类分析不同的是,判别分析时组别的特征已知. 定义:判别分析先根据已知类别的事物的性质,利用某种技术建立函数式,然后对未知类别的新事物进 行判 ...

  10. Java NIO Tutorial

    Java NIO Tutorial     Jakob JenkovLast update: 2014-06-25