hashcode

数据结构解析-HashMap

回眸只為那壹抹淺笑 提交于 2019-12-06 06:28:11
概要 HashMap在JDK1.8之前的实现方式 数组+链表,但是在JDK1.8后对HashMap进行了底层优化,改为了由 数组+链表+红黑树实现,主要的目的是提高查找效率。 如图所示: JDK版本 实现方式 节点数>=8 节点数<=6 1.8以前 数组+单向链表 数组+单向链表 数组+单向链表 1.8以后 数组+单向链表+红黑树 数组+红黑树 数组+单向链表 HashMap 1.继承关系 public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable 2.常量&构造方法 //这两个是限定值 当节点数大于8时会转为红黑树存储 static final int TREEIFY_THRESHOLD = 8; //当节点数小于6时会转为单向链表存储 static final int UNTREEIFY_THRESHOLD = 6; //红黑树最小长度为 64 static final int MIN_TREEIFY_CAPACITY = 64; //HashMap容量初始大小 static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16 //HashMap容量极限 static final int

Java String hashcode caching

南笙酒味 提交于 2019-12-06 05:57:39
问题 One of the advantages of String immutability is hashcode caching for faster access. In this case how cache is handled for string which has same hashcode? Does it really improve the performance in this case? 回答1: In this case how cache is handled for string which has same hashcode? I don't understand the first part of your question. Cache is handled for all Strings the same, whether hashcodes are the same or not (since two different Strings can theoretically have the same hashCode, and so if

How likely is it to get a HashCode collision with this hashcode function?

痞子三分冷 提交于 2019-12-06 03:37:10
问题 How likely is it to get a HashCode collision with the function below in following scenarios. With random int values for key[0],key[1], key[2], key[3] With random key values with the following constraints key[0] <1,000,000 key[1] <10,000 key[2] <1,000 key[3] <1,000 Assume we have 10 Million objects. int[] key=new int[4]; public override int GetHashCode() { // Use large prime multiples to create a unique hash key // Create the hash offsets using a "even powers of 2 minus 1" method, which gives

Java: Modify id that changes hashcode

混江龙づ霸主 提交于 2019-12-06 03:19:42
问题 I use HashSet and I need to modify the ID of an object, but it changes hashcode and breaks HashSet and rules of hashCode() method. What is best solution: to delete object from Set and add object with new ID, or to keep the hash code (generated in constructor, for example) in every object in Set, or is there other way to solve this problem? Thanks for help. UPDATE: I made mistake: keeping hash code in object is terrible, because in that case equal objects can have different hash codes. 回答1: A

How to get a hash code as integer in R?

我们两清 提交于 2019-12-06 03:17:25
问题 What I want to do is implement a hash trick in R. Code below: library(digest) a<-digest("key_a", algo='xxhash32') #[1] "4da5b0f8" This returned a hash code in a character type. Is there any way I can turn it into a integer? Or is there any other package to achieve this? 回答1: That output is a hex (base 16) string. Use following function to change it to decimal. Taken from another forum post but link does not work anymore (2017). hex_to_int = function(h) { xx = strsplit(tolower(h), "")[[1L]]

Hashcode implementation double precision

人走茶凉 提交于 2019-12-06 02:36:44
问题 I've asked a question about this class before, but here is one again. I've created a Complex class: public class Complex { public double Real { get; set; } public double Imaginary { get; set; } } And I'm implementing the Equals and the Hashcode functions, and the Equal function takes in account a certain precision. I use the following logic for that: public override bool Equals(object obj) { //Some default null checkint etc here, the next code is all that matters. return Math.Abs(complex

一个小案例来理解实体类中重写equals()和hashCode()方法的必要性

一世执手 提交于 2019-12-06 02:09:09
今天在使用list的indexOf()方法时,自己新添加的一个对象,通过list的indexOf来查看list里面有没有这个对象,但是无论怎么尝试都返回-1(无法获取到指定的对象),后来发现是实体类对象没有重写equals()和hashCode()方法。代码的案例如下: 1.实体类emp如下: public class Employee{ private int id; private int name; //构造方法 public Employee(String name){ this.name =name; } //省略get,set方法... } 2.写一个测试方法,我们希望手动添加一个Emp对象,通过list的indexOf方法来找到已有的emp对象name相同的emp对象。 public static void main(String[] args) { Employee emp1 = new Employee();//创建第一个员工 emp1.setName("test1"); Employee emp2 = new Employee();//创建第二个员工 emp2.setName("test2"); List<Employee> emps = new ArrayList<>();//将员工存入到list emps.add(emp1); emps.add(emp2);

Hash Codes for Floats in Java

可紊 提交于 2019-12-06 01:43:41
I have a class with two float variables and hashCode method (without equals in current code snippet): public class TestPoint2D { private float x; private float z; public TestPoint2D(float x, float z) { this.x = x; this.z = z; } @Override public int hashCode() { int result = (x != +0.0f ? Float.floatToIntBits(x) : 0); result = 31 * result + (z != +0.0f ? Float.floatToIntBits(z) : 0); return result; } } The following test @Test public void tempTest() { TestPoint2D p1 = new TestPoint2D(3, -1); TestPoint2D p2 = new TestPoint2D(-3, 1); System.out.println(p1.hashCode()); System.out.println(p2

Why and how does HashMap have its own internal implementation of hashCode() called hash()?

南笙酒味 提交于 2019-12-05 22:43:05
问题 According to this blog entry, HashMap reinvokes its own implementation of hashCode() (called hash() ) on a hashcode it already retrieved. If key is not null then , it will call hashfunction on the key object , see line 4 in above method i.e. key.hashCode() ,so after key.hashCode() returns hashValue , line 4 looks like int hash = hash(hashValue) and now ,it applies returned hashValue into its own hashing function . We might wonder why we are calculating the hashvalue again using hash(hashValue

浅谈HashMap

≡放荡痞女 提交于 2019-12-05 22:40:02
第一次写随笔,之前看了不少有关HashMap的内容,看完一会就忘了,还是需要梳理消化一下,写下来。HashMap是一个我们经常用的数据类型,网上也有很多相关的内容,而且面试也经常会问到,它是JAVA基础比较重要的知识点。 原文链接: https://www.cnblogs.com/williamjie/p/9358291.html 1、hashmap的数据结构 要知道hashmap是什么,首先要搞清楚它的数据结构,在java编程语言中,最基本的结构就是两种,一个是数组,另外一个是模拟指针(引用),所有的数据结构都可以用这两个基本结构来构造的,hashmap也不例外。Hashmap实际上是一个数组和链表的结合体(在数据结构中,一般称之为“链表散列“),请看下图(横排表示数组,纵排表示数组元素【实际上是一个链表】)。 由图知,HashMap底层是一个 Entry数组 ,每一个Entry数组元素对应 一个链表 (JAVA8中链表长度超过8时就会把链表转换成红黑树)。 在map. put ("a",1)的时候发生了什么呢?首先插入一个KEY为"a"的元素,在根据哈希函数找到存储的位置:index = Hash("a")。假设算出来Hash("a") = 2,则把Entry插进去index = 2的数组里面。 如果已经有值了,则出现hash冲突问题