hashmap

HashMap holding duplicate keys

强颜欢笑 提交于 2019-12-31 04:07:09
问题 While experimenting with HashMap , I noticed something weird. Ran 4 threads with each trying to put (key,value) with keys from 0 to 9999, value a constant string. After all threads were done, map.size() returned a value greater than 10,000. How did this happen? Does this mean that the map contains duplicate keys? I iterated on the map.entrySet() and found that the count for some keys were indeed more than 1. What value would be returned if I do a get() on the map for one such key. Here is the

Getting stackoverflowerror from Gson while converting hashmap to JSON object

泄露秘密 提交于 2019-12-31 03:05:09
问题 I want to represent data in tree structure as java object then I want to convert it to JSON object. With the help of stackoverflow entries: Convert java arrayList of Parent/child relation into tree? hashmap to JSON using GSON I had below main function and "pairs" list contains a pair: child and parent ArrayList<Pair> list= new ArrayList<>(); list.add(new Pair("6", "4")); list.add(new Pair("5", "4")); list.add(new Pair("4", "3")); list.add(new Pair("2", "3")); list.add(new Pair("3", "null"));

Java - How to find a value from a hashmap that is the closest to a particular number?

為{幸葍}努か 提交于 2019-12-31 01:59:04
问题 Hi I have a HashMap<String, Double> and also a function which returns a double value known as answer . I want to check which value in the HashMap is the closest to the answer and then grab that value's key and print it. HashMap<String, Double> output = new HashMap<String, Double>(); contents ("A", 0) ("B", 0.25) ("C", 0.5) ("D", 0.75) ("E", 1) Suppose the answer to one of my functions was 0.42, how can I check which value it is closest to and then grab the key to that value. I cant switch

JSON Decode Custom Class with HashMap member using GSON in Java

…衆ロ難τιáo~ 提交于 2019-12-31 01:52:27
问题 I have the following class: class IndexItem { private String word; private HashMap<String, Integer> docs; private Integer total; public IndexItem(String word) { this.total = 0; this.docs = new HashMap<String, Integer>(); this.word = word; } public IndexItem() { this.total = 0; this.docs = new HashMap<String, Integer>(); this.word = ""; } } I also have the following JSON string encoded from one of this classes instances using GSON: {"word":"refer","docs":{"c84ada58bb47e7ee8fab14d6d0ae1978.html

图解数据结构(04) -- 哈希表

左心房为你撑大大i 提交于 2019-12-30 23:08:15
哈希表 1、什么是哈希表 2、哈希函数 哈希函数的实现 3、哈希表的读写操作 写操作(put) 读操作(get) 扩容(resize) 4、总结 1、什么是哈希表 哈希表(hash table),这种数据结构提供了键(Key)和值 (Value)的映射关系;只要给出一个Key,就可以高效查找到它所匹配的Value,时间复杂度接近于O(1) 2、哈希函数 散列表在本质上也是一个数组,可是数组只能根据下标,像a[0]、a[1]、a[2]、a[3]、a[4]这样来访问,而散列表的Key则是以字符串类型为主的,例如以学生的学号作为Key,输入002123,查询到李四;或者以单词为Key,输入by,查询到数字46……所以需要一个“中转站”,通过某种方式, 把Key和数组下标进行转换,这个中转站就叫作哈希函数。 哈希函数的实现 以Java的常用集合HashMap为例,来讲解哈希函数在Java中的实现: 在Java及大多数面向对象的语言中,每一个对象都有属于自己的hashcode,这个hashcode是区分不同对象的重要标识,无论对象自身的类型是什么,它们的 hashcode都是一个整型变量。 既然都是整型变量,想要转化成数组的下标简单的转化方式就是按照数组长度进行 取模运算 index = HashCode (Key) % Array.length 通过哈希函数*

Why use EnumMap instead of HashMap

杀马特。学长 韩版系。学妹 提交于 2019-12-30 18:28:35
问题 As we already have HashMap , why would we use EnumMap ? 回答1: The Javadoc makes a pretty good argument: Enum maps are represented internally as arrays. This representation is extremely compact and efficient. Implementation note: All basic operations execute in constant time. They are likely (though not guaranteed) to be faster than their HashMap counterparts. 回答2: The main reason for EnumMap is that it is specifically optimised for enums. Further benefits are mentioned below. Taken help from

Mybatis null value for resultype hashmap

廉价感情. 提交于 2019-12-30 12:38:30
问题 <select id="getData" parameterType="Object" resultType="map"> select query.* from (${query}) query </select> So this is the query I want to execute. Although the query is correct and returning the desired data, but when the data is inserted in the resultType map as specified the null values present in some of the columns get omitted. I have searched/googled for the same, but not got what I want. I also viewed this link, but can't get my answer. Is there any way to get those omitted

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;//用于指向链表的下一层(产生冲突,用拉链法) }