hashcode

HashMap、lru、散列表

北战南征 提交于 2020-01-01 10:21:05
HashMap HashMap的数据结构:HashMap实际上是一个数组和链表(“链表散列”)的数据结构。底层就是一个数组结构,数组中的每一项又是一个链表。 hashCode是一个对象的标识,Java中对象的hashCode是一个int类型值。通过hashCode来算出指定数组的索引可以快速定位到要找的对象在数组中的位置,之后再遍历链表找到对应值,理想情况下时间复杂度为O(1),并且不同对象可以拥有相同的hashCode(hash碰撞)。发生碰撞后会把相同hashcode的对象放到同一个链表里,但是在数组大小不变的情况下,存放键值对越多,查找的时间效率也会降低 扩容可以解决该问题,而负载因子决定了什么时候扩容,负载因子是已存键值对的数量和总的数组长度的比值。默认情况下负载因子为0.75,我们可在初始化HashMap的时候自己修改。阀值 = 当前数组长度✖负载因子 hashmap中默认负载因子为0.75,长度默认是16,默认情况下第一次扩容判断阀值是16 ✖ 0.75 = 12;所以第一次存键值对的时候,在存到第13个键值对时就需要扩容了,变成16X2=32。 put流程 对key hash,二次hash,hash扰乱函数,减少hash碰撞 int hash(Object key) { int h = key.hashCode(); return (h ^ (h >>> 16)) &

Equals for case class with floating point fields

主宰稳场 提交于 2020-01-01 06:25:30
问题 Is it ok, to create case classes with floating point fields, like: case class SomeClass(a:Double, b:Double) I guess auto generated equal method won't work in this case. Is overriding equals the best solution? EDIT: if overriding equals is the way to go, I would like to avoid hardcoding epsilon ( where epsilon is defined like => |this.a-a|< epsilon). This won't compile: case class SomeClass(a:Double, b:Double, implicit epsilon:Double) I am looking for a way to pass epsilon without passing

What's a good recipe for overriding hashcode in Dart?

时间秒杀一切 提交于 2020-01-01 01:12:23
问题 I find myself wanting to override hashcode and == for an object, and I'm wondering if there are best practices for how to implement a hashcode that depends on multiple attributes, and it seems like there are some Dart-specific considerations. The simplest answer would be to XOR the hashes of all the attributes together, and it's probably not too bad. There's also an example in Dart Up and Running at https://www.dartlang.org/docs/dart-up-and-running/contents/ch03.html // Override hashCode

Is it proper for equals() to depend only on an ID?

て烟熏妆下的殇ゞ 提交于 2020-01-01 01:12:13
问题 Let's suppose I have class User : public class User { private Long id; private String name; private Integer age; private BigDecimal account; // other fields, getters and setters } Is it proper to override the equals method as follows? @Override public boolean equals(Object ob) { if (ob == null) { return false; } if (this == ob) { return true; } if (ob instanceof User) { User other = (User) ob; return this.id.equals(other.getId()); } return false; } It turns out that the uniqueness of an

JDK1.7 的 HashMap

南楼画角 提交于 2019-12-30 12:19:09
HashMap是一个用于存储key-value的键值对集合,每个键值对都是一个Entry。这些键值对分散存储在一个数组中,这个数组就是HashMap的主干。 HashMap每个初始值都为null。 1.Put方法的原理 调用Put方法的时候发生了什么呢? 比如调用 hashMap.put("apple", 0) ,插入一个Key为“apple"的元素。这时候我们需要利用一个哈希函数来确定Entry的插入位置(index): index = Hash(“apple”) 假定最后计算出的index是2,那么结果如下: 但是,因为HashMap的长度是有限的,当插入的Entry越来越多时,再完美的Hash函数也难免会出现index冲突的情况。比如下面这样: 这时候该怎么办呢?我们可以利用 链表 来解决。 HashMap数组的每一个元素不止是一个Entry对象,也是一个链表的头节点。每一个Entry对象通过Next指针指向它的下一个Entry节点。当新来的Entry映射到冲突的数组位置时,只需要插入到对应的链表即可: 需要注意的是,新来的Entry节点插入链表时,使用的是“头插法”。至于为什么不插入链表尾部,后面会有解释。 HashMap的初始化长度16,每次自动扩容或者手动扩容长度必须是2的幂。 之前说过,从Key映射到HashMap数组的对应位置,会用到一个Hash函数: index

HashMap 内部原理

孤人 提交于 2019-12-30 12:11:33
HashMap 内部实现 通过名字便可知道的是,HashMap 的原理就是散列。HashMap内部维护一个 Buckets 数组。每一个 Bucket 封装为一个 Entry<K, V> 键值对形式的链表结构。这个 Buckets 数组也称为表。表的索引是 密钥 K 的散列值(散列码)。 例如以下图所看到的: 链表的每一个节点是一个名为 Entry<K,V> 的类的实例。 Entry 类实现了 Map.Entry 接口,以下是Entry类的代码: private static class Entry<K,V> implements Map.Entry<K,V> { final K key; final int hash; V value; Entry<K,V> next; } 注: 每一个 Entry 对象仅与一个特定 key 相关联。但其 value 是能够改变的(假设同样的 key 之后被又一次插入不同的 value) - 因此键是终于的,而值不是。 每一个Entry对象都有一个名为 next 的字段,它指向下一个Entry,所以实际上为单链表结构。hash 字段存储了 Entry 对象在 Buckets 数组索引,也就是 key 的散列值。 假设发生Hash碰撞,也就是两个key的hash值同样,或者假设这个元素所在的位子上已经存放有其它元素了

深入理解HashMap

淺唱寂寞╮ 提交于 2019-12-30 12:11:10
1、为什么用HashMap? HashMap是一个散列桶(数组和链表),它存储的内容是键值对(key-value)映射 HashMap采用了数组和链表的数据结构,能在查询和修改方便继承了数组的线性查找和链表的寻址修改 HashMap是非synchronized,所以HashMap很快 HashMap可以接受null键和值,而Hashtable则不能(原因就是equlas()方法需要对象,因为HashMap是后出的API经过处理才可以) 2、HashMap的工作原理是什么? HashMap是基于hashing的原理,我们使用put(key, value)存储对象到HashMap中,使用get(key)从HashMap中获取对象。当我们给put()方法传递键和值时,我们先对键调用hashCode()方法,计算并返回的hashCode是用于找到Map数组的bucket位置来储存Node 对象。这里关键点在于指出,HashMap是在bucket中储存键对象和值对象,作为Map.Node 。   以下是HashMap初始化 ,简单模拟数据结构 Node[] table=new Node[16] 散列桶初始化,table class Node { hash;//hash值 key;//键  value;//值  node next;//用于指向链表的下一层(产生冲突,用拉链法) }

Is Dictionary broken or should GetHashCode() only base on immutable members?

不问归期 提交于 2019-12-30 08:04:40
问题 When an object is added to the .NET System.Collections.Generic.Dictionary class the hashcode of the key is stored internally and used for later comparisons. When the hashcode changes after its initial insertion into the dictionary it often becomes "inaccessible" and may surprise its users when an existence check, even using the same reference, returns false (sample code below). The GetHashCode documentation says: The GetHashCode method for an object must consistently return the same hash code

Why is the default hashcode() in java bad?

我的未来我决定 提交于 2019-12-30 05:08:10
问题 if I remember correctly the default hashCode() implementation in java of an object of type Object() is to return the memory address of the object. When we create our own classes, I read that we want to override hashCode() so that when we insert them into a hash related collection like HashMap() it will work properly. But why is a memory address bad? Sure we will EVENTUALLY run out of memory and you'll have collisions, but the only case where I see this being a problem is where you are dealing

java 浅拷贝 深拷贝

随声附和 提交于 2019-12-30 04:21:17
一,问题背景 最近遇到一块代码,看了半天没有看明白如何实现树形结构的。debugger 以 后发现原理,其实是利用了java对象是引用类型,利用浅拷贝来实现树型结构。 /** * * @param table "树型结构中的所有节点" * @param childrenField "固定key,名称为children" * @param idField "每个节点id" * @param parentIdField "子节点与父节点的关系属性parentId" * @return */ public static ArrayList list2Tree(List table, String childrenField, String idField, String parentIdField) { ArrayList tree = new ArrayList(); Map hash = new HashMap();//装载所有对象 id,object格式 for (int i = 0, l = table.size(); i < l; i++) { Map t = (Map)table.get(i); hash.put(t.get(idField), t); } for (int i = 0, l = table.size(); i < l; i++) { Map t = (Map