hashmap

Java集合之HashSet源码分析

[亡魂溺海] 提交于 2020-01-01 14:56:22
一、HashSet简介   HashSet是Set接口典型实现,它按照Hash算法来存储集合中的元素,具有很好的存取和查找性能。主要具有以下特点: 不保证set的迭代顺序 HashSet不是同步的,如果多个线程同时访问一个HashSet,要通过代码来保证其同步 集合元素值可以是null   当向HashSet集合中存入一个元素时,HashSet会调用该对象的hashCode()方法来得到该对象的hashCode值,然后根据该值确定对象在HashSet中的存储位置。在Hash集合中,不能同时存放两个相等的元素,而判断两个元素相等的标准是两个对象通过equals方法比较相等并且两个对象的HashCode方法返回值也相等。   下面的例子说明了上述特性: public class Person { String name; int age; public Person(String name,int age) { this.name=name; this.age=age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age

Java集合之HashSet

笑着哭i 提交于 2020-01-01 14:56:07
1.HashSet概述:   HashSet实现Set接口,由哈希表(实际上是一个HashMap实例)支持。 它不保证set 的迭代顺序;特别是它不保证该顺序恒久不变。此类允许使用null元素。HashSet中不允许有重复元素,这是因为HashSet是基于HashMap实现的,HashSet中的元素都存放在HashMap的 key 上面,而value中的值都是统一的一个 private static final Object PRESENT = new Object(); 。HashSet跟HashMap一样,都是一个存放链表的数组。   HashSet中add方法调用的是底层HashMap中的put()方法,而如果是在HashMap中调用put,首先会判断key是否存在,如果key存在则修改value值,如果key不存在这插入这个key-value。而在set中,因为value值没有用,也就不存在修改value值的说法, 因此往HashSet中添加元素,首先判断元素(也就是key)是否存在,如果不存在这插入,如果存在着不插入,这样HashSet中就不存在重复值。 2.HashSet的实现: 对于HashSet而言,它是 基于HashMap实现的 , HashSet底层使用HashMap来保存所有元素,更确切的说,HashSet中的元素,只是存放在了底层HashMap的 key 上

java集合HashMap、HashTable、HashSet详解

99封情书 提交于 2020-01-01 14:55:55
一、Set和Map关系 Set代表集合元素无序,集合元素不可重复的集合,Map代表一种由多个key-value组成的集合,map集合是set集合的扩展只是名称不同,对应如下 二、HashMap的工作原理 HashMap基于 hashing原理 ,通过put()和get()方法储存和获取对象。 put()方法: 它调用键对象的hashCode()方法来计算hashcode值,系统根据hashcode值决定该元素在bucket位置。如果两个对象key的hashcode返回值相同,那他们的存储位置相同,如果这两个Entry的key通过equals比较返回true,新添加Entry的value将覆盖集合中原有Entry的value,但key不会覆盖;如果这两个Entry的key通过equals比较返回false,新添加的Entry将与集合中原有Entry形成Entry链,而且新添加Entry位于Entry链的头部。put源码如下: public V put(K paramK, V paramV) { //如果key为空,调用putForNullKey方法 if (paramK == null) return putForNullKey(paramV); //根据key的keyCode计算Hash值 int i = hash(paramK.hashCode()); /

hashmap containsKey complexity

隐身守侯 提交于 2020-01-01 14:22:37
问题 I have a method i wrote to find the duplicates in a List. It works fine but im concerned about the complexity of using containsKey. When we use containsKey we have to compute a hash function for every key and then compare against each with our search item, right ? So wouldn't the complexity be O(n) ? Here is the function: public void findDup(List<String> list){ HashMap<String,Integer> map = new HashMap<>(); int pos=0; for(String s: list){ if(map.containsKey(s)){ Log.v("myapp","duplicate found

Java hashmap readonly thread safety

久未见 提交于 2020-01-01 12:09:38
问题 I have this code that has a shared hash map initialized in static block. I don't expose the hashmap and it's used read-only (get and containKey). I wanted to make sure if this is thread-safe. public class MyClass { private static final Map<String, MyObject> myMap; static { myMap = new MyLoader().load() } public MyClass() { if (containsKey(someKey)) { // do something } myMap.get(something) } static boolean containsKey(String key) { // do some other stuff return myMap.containsKey(key) } } 回答1:

Error converting JSON string to map in Java using Jackson

你。 提交于 2020-01-01 10:57:28
问题 I have this little piece of code, and I'm trying to convert a JSON string to a map. String json = "[{'code':':)','img':'<img src=/faccine/sorriso.gif>'}]"; ObjectMapper mapper = new ObjectMapper(); Map<String,String> userData = mapper.readValue(json,new TypeReference<HashMap<String,String>>() { }); But it returns the following error: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.HashMap out of START_ARRAY token at [Source: java.io.StringReader

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)) &

Usage of WeakHashMap? [duplicate]

天大地大妈咪最大 提交于 2020-01-01 10:04:49
问题 This question already has answers here : When would you use a WeakHashMap or a WeakReference? (10 answers) Closed last year . WeakHashMap is an implementation of Map interface where the memory of the value object can be reclaimed by Grabage Collector if the corresponding key is no longer referred by any section of program. So if key is no longer used in program. its Entry object will be garbage collected irrespective of its usage. Its clear till here This is different from HashMap where the

Data structure for efficiently returning the top-K entries of a hash table (map, dictionary)

家住魔仙堡 提交于 2020-01-01 09:23:08
问题 Here's a description: It operates like a regular map with get , put , and remove methods, but has a getTopKEntries(int k) method to get the top-K elements, sorted by the key: For my specific use case, I'm adding, removing, and adjusting a lot of values in the structure, but at any one time there's approximately 500-1000 elements; I want to return the entries for the top 10 keys efficiently. I call the put and remove methods many times. I call the getTopKEntries method. I call the put and

Is it a bug that Java 8's HashMap misbehaves if the keys implement Comparable in a way that isn't consistent with equals?

ぃ、小莉子 提交于 2020-01-01 08:38:28
问题 I know that since Java 8, if a HashMap has enough hash collisions, and the keys implement Comparable , it will use a balanced tree instead of a linked list for the bin. But from what I can see, the Comparable interface does not require that compareTo() be "consistent with equals() " (though it is strongly recommended). Did I miss something? It seems like the new implementation allows HashMap to violate the requirements of the Map interface if the keys happen to have a compliant, but non