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

原文地址:https://www.cnblogs.com/immiao0319/p/9841048.html

时间: 2024-07-31 22:23:19

Hashed collections哈希集合的相关文章

第15章 hash_set哈希集合容器

/* 第15章 hash_set哈希集合容器   15.1 hash_set技术原理   15.2 hash_set应用基础   15.3 本章小结 略 */

[Guava学习笔记]Collections: 不可变集合, 新集合类型

不可变集合 不接受null值. 创建:ImmutableSet.copyOf(set); ImmutableMap.of(“a”, 1, “b”, 2); public static final ImmutableSet<Color> GOOGLE_COLORS = ImmutableSet.<Color>builder() .addAll(WEBSAFE_COLORS) .add(new Color(0, 191, 255)) .build(); 可以有序(如ImmutableS

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

Design a HashSet without using any built-in hash table libraries. To be specific, your design should include these functions: add(value): Insert a value into the HashSet. contains(value) : Return whether the value exists in the HashSet or not. remove

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

1.特点:HashSet 中元素不重复,容量为元素个数,自动增大.是一组值,是高性能的数学集合. 2.创建: (1)HashSet<类型> 集合名 = new HashSet<类型>(): //空集合 (2)HashSet<类型> 集合名 = new HashSet<类型>() { "马超", "关羽" }; (3)HashSet<类型> 集合名 = new HashSet<类型>(数组名);/

705.设计哈希集合

2020-04-07 设计哈希集合 不使用任何内建的哈希表库设计一个哈希集合 具体地说,你的设计应该包含以下的功能 add(value):向哈希集合中插入一个值. contains(value) :返回哈希集合中是否存在这个值. remove(value):将给定值从哈希集合中删除.如果哈希集合中没有这个值,什么也不做. 题解: 思路1: 使用数组设计哈希集合 (不建议) 之所以不建议是因为性能差 每次add,contains或者delete都要遍历看看这个值是不是存在 // 使用数组做hash

java 18-11 Collections用于ArrayList集合中

Collections可以针对ArrayList存储基本包装类的元素排序,存储自定义对象可不可以排序呢? 自定义对象要自己写比较器进行排序 1 package cn.itcast_02; 2 3 import java.util.ArrayList; 4 import java.util.Collections; 5 import java.util.Comparator; 6 import java.util.List; 7 public class CollectionsDemo { 8 p

Collections专门操作集合的类

sort    排序 binarySearch    查找 fill    改变列表的所有 replaceAll    覆盖所有相同的 reverse    反转集合内容 swap    交换操作 shuffle    随机分配,就是打乱顺序 Comparator    外部比较器的使用 import java.util.*; public class anli { public static void main (String[] args) { // binarySearchdemo();

LeetCode 705. Design HashSet (设计哈希集合)

题目标签:HashMap 题目让我们设计一个 hashset,有add,contains,remove 功能. 建立一个boolean array,index 是数字的值,具体看code. Java Solution: Runtime: 58 ms, faster than 90.21% Memory Usage: 56.3 MB, less than 68.53% 完成日期:03/18/2019 关键点:boolean array class MyHashSet { boolean [] se

20_集合_第20天(Map、可变参数、Collections)_讲义

今日内容介绍 1.Map接口 2.模拟斗地主洗牌发牌 01Map集合概述 A:Map集合概述: 我们通过查看Map接口描述,发现Map接口下的集合与Collection接口下的集合,它们存储数据的形式不同 ? a:Collection中的集合,元素是孤立存在的(理解为单身),向集合中存储元素采用一个个元素的方式存储. ? b:Map中的集合,元素是成对存在的(理解为夫妻).每个元素由键与值两部分组成,通过键可以找对所对应的值. ? Collection中的集合称为单列集合,Map中的集合称为双列