hashmap

hashcode()方法

妖精的绣舞 提交于 2019-12-10 08:08:38
 浅谈Java中的hashcode方法   哈希表这个数据结构想必大多数人都不陌生,而且在很多地方都会利用到hash表来提高查找效率。在Java的Object类中有一个方法: public native int hashCode();   为何Object类需要这样一个方法?它有什么作用呢?今天我们就来具体探讨一下hashCode方法。  根据这个方法的声明可知,该方法返回一个int类型的数值,并且是本地方法,因此在Object类中并没有给出具体的实现。 一.hashCode方法的作用   对于包含容器类型的程序设计语言来说,基本上都会涉及到hashCode。在Java中也一样,hashCode方法的主要作用是为了配合基于散列的集合一起正常运行,这样的散列集合包括HashSet、HashMap以及HashTable。   为什么这么说呢?考虑一种情况,当向集合中插入对象时,如何判别在集合中是否已经存在该对象了?(注意:集合中不允许重复的元素存在)   也许大多数人都会想到调用equals方法来逐个进行比较,这个方法确实可行。但是如果集合中已经存在一万条数据或者更多的数据,如果采用equals方法去逐一比较,效率必然是一个问题。此时hashCode方法的作用就体现出来了,当集合要添加新的对象时,先调用这个对象的hashCode方法,得到对应的hashcode值

hashmap原理

試著忘記壹切 提交于 2019-12-10 08:08:25
1.hashmap结构(jdk1.8) 结构:hashmap采用数组+链表+红黑树的结构, 数组用查询快的特点,但是插入数据慢,链表有插入快的特点,但是查询慢,所以hashmap结合两者,而红黑树是为了链表过长影响查询速度,将链表转化为红黑树. 源码: //hashmap默认初始化容量 static final int DEFAULT_INITIAL_CAPACITY = 1 << 4 ; // aka 16 //最大容量 static final int MAXIMUM_CAPACITY = 1 << 30 ; //默认填充因子 static final float DEFAULT_LOAD_FACTOR = 0.75f ; //当大于时将链表转化为红黑树 static final int TREEIFY_THRESHOLD = 8 ; //当小于时,将树转化为链表 static final int UNTREEIFY_THRESHOLD = 6 ; //当table个数大于等于时才能转化为红黑树 static final int MIN_TREEIFY_CAPACITY = 64 ; //包含key-value个数 transient int size ; //hashmap被修改次数 transient int modCount ; //要调整大小的下一个大小值(容量

Laziness of eviction in Guava's maps

只谈情不闲聊 提交于 2019-12-10 07:46:57
问题 Current eviction algorithm for maps is quite lazy. It looks like expired objects are evicted only when the data structure is accessed. For example, the map from addresses to indexers defined as: ConcurrentMap<Address, Indexer> indexers = new MapMaker() .expireAfterAccess( EXPIRATION, TimeUnit.SECONDS) .evictionListener( new IndexEvicted()) .makeMap(); leads to quite surprising pattern: while containsKey() for the given address returns false, immediately after that indexer for that address is

深入分析 JDK8 中 HashMap 的原理、实现和优化

久未见 提交于 2019-12-10 05:09:16
HashMap 可以说是使用频率最高的处理键值映射的数据结构,它不保证插入顺序,允许插入 null 的键和值。本文采用 JDK8 中的源码,深入分析 HashMap 的原理、实现和优化。首发于微信公众号 顿悟源码 . 1. 基本结构 HashMap 基于散列表实现,使用 拉链法 处理碰撞,在 JDK8 中,当链表长度大于 8 时转为 红黑树 存储,基本结构如下: HashMap 有一个 Node<K,V>[] table 字段,即哈希桶数组,数组元素是 Node 对象,结构定义如下: static class Node<K,V> implements Map.Entry<K,V> { final int hash; // 用于计算数组索引 final K key; V value; Node<K,V> next; // 后继节点,下一个 Node Node(int hash, K key, V value, Node<K,V> next) { ... } ... } 哈希桶数组会在首次使用时初始化,默认大小是 16,并根据需要调整大小,且长度总是 2 的次幂。如果构造函数设置的初始容量不是 2 的次幂,那么使用以下方法返回一个 大于且最靠近 它的 2 的次幂的值: static final int tableSizeFor(int cap) { int n = cap - 1; n

What load factor should be used when you know maximum possible no of elements in HashSet

孤街醉人 提交于 2019-12-10 04:17:29
问题 What load factor should I use when I really know the maximum possible no of elements in a HashSet ? I had heard that the default load factor of 0.75 is recommended as it offers good performance trade-offs between speed & space. Is this correct ? However a larger size HashSet would also takes more time in creation and more space. I am using HashSet just inorder to remove duplicate integers from a list of integers. 回答1: I spent some time playing around with load factors once, and it is shocking

Java: Arrays & Vectors

假装没事ソ 提交于 2019-12-10 04:15:07
问题 I'm used to working with PHP but lately I've been working with Java and I'm having a headache trying to figure this out. I want to save this representation in Java: Array ( ["col_name_1"] => Array ( 1 => ["col_value_1"], 2 => ["col_value_2"], ... , n => ["col_value_n"] ), ["col_name_n"] => Array ( 1 => ["col_value_1"], 2 => ["col_value_2"], ... , n => ["col_value_n"] ) ) Is there a clean way (i.e. no dirty code) to save this thing in Java? Note; I would like to use Strings as array indexes

Sorting a HashMap by date

独自空忆成欢 提交于 2019-12-10 03:57:59
问题 In a Java class I have a method to reOrder an existing HashMap by date. The HashMap is of a type <String, Object> where the Object contains a field called expPayDate and the key string is a sequential number turned into a string.. So I need to loop through the items in the sourceMap and find the item with the newest date then copy it to a tempMap in the correct order. My issue is what is the best way to determine the item with the newest date. 回答1: Use a TreeMap instead of HashMap. it will be

Why HashMap does not guarantee that the order of the map will remain constant over time

浪子不回头ぞ 提交于 2019-12-10 03:32:18
问题 I was reading about difference between Hashmap and Hashtable here: http://javarevisited.blogspot.sg/2010/10/difference-between-hashmap-and.html Can anyone throw some light on why it says following? "5. HashMap does not guarantee that the order of the map will remain constant over time." Could the order change during re-hashing, that is why? It would be also nice if you could point me to resource or list of collections who exhibit such behavior of not guaranteeing order to be remain constant.

java8: hashmap性能提升

扶醉桌前 提交于 2019-12-10 03:28:54
HashMap是一个高效通用的数据结构,它在每一个Java程序中都随处可见。先来介绍些基础知识。你可能也知道,HashMap使用key的hashCode()和equals()方法来将值划分到不同的桶里。桶的数量通常要比map中的记录的数量要稍大,这样每个桶包括的值会比较少(最好是一个)。当通过key进行查找时,我们可以在常数时间内迅速定位到某个桶(使用hashCode()对桶的数量进行取模)以及要找的对象。 这些东西你应该都已经知道了。你可能还知道哈希碰撞会对hashMap的性能带来灾难性的影响。如果多个hashCode()的值落到同一个桶内的时候,这些值是存储到一个链表中的。最坏的情况下,所有的key都映射到同一个桶中,这样hashmap就退化成了一个链表——查找时间从O(1)到O(n)。我们先来测试下正常情况下hashmap在Java 7和Java 8中的表现。为了能完成控制hashCode()方法的行为,我们定义了如下的一个Key类: class Key implements Comparable<Key> {private final int value;Key(int value) {this.value = value;}@Overridepublic int compareTo(Key o) {return Integer.compare(this.value, o

hashmap和hash算法研究

余生长醉 提交于 2019-12-10 00:21:13
总述   hashmap作为java中非常重要的数据结构,对于key-value类型的存储(缓存,临时映射表,。。。)等不可或缺,hashmap本身是非线程安全的,对于多线程条件下需要做竞争条件处理,可以通过Collections和ConcurrentHashmap来替代。   Hashmap源码探究   数据结构   hashmap存储数据主要是通过数组+链表实现的,通过将key的hashcode映射到数组的不同元素(桶,hash中的叫法),然后冲突的元素放入链表中。      链表结构(Entry)      采用静态内部类   存储操作      当存入的值为null的时候,操作会找到table [0] ,set key为null的值为新值   若果不是空值,则进行hash,   hash算法      可以看到,算法进行了二次hash,使高位也参与到计算中,防止低位不变造成的hash冲突   注:这一切的目的实际上都是为了使value尽量分布到不同的   对于任意给定的对象,只要它的 hashCode() 返回值相同,那么程序调用 hash(int h) 方法所计算得到的 hash 码值总是相同的。我们首先想到的就是把 hash 值对数组长度取模运算,这样一来,元素的分布相对来说是比较均匀的。但是,“模”运算的消耗还是比较大的,在 HashMap 中是这样做的:调用