hashmap

List、set、Map的底层实现原理

和自甴很熟 提交于 2019-12-10 00:16:58
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/xzp_12345/article/details/79251174 本文主要是参考了网上一些对java集合讲解的比较详细博客进行归纳总结,下面对 java集合中几个比较常用的类归纳分析。 ArrayList实现原理要点概括 参考文献: http://zhangshixi.iteye.com/blog/674856l https://www.cnblogs.com/leesf456/p/5308358.html ArrayList是List接口的可变数组非同步实现,并允许包括null在内的所有元素。底层使用数组实现该集合是可变长度数组,数组扩容时,会将老数组中的元素重新拷贝一份到新的数组中,每次数组容量增长大约是其容量的1.5倍,这种操作的代价很高。采用了Fail-Fast机制,面对并发的修改时,迭代器很快就会完全失败,而不是冒着在将来某个不确定时间发生任意不确定行为的风险remove方法会让下标到数组末尾的元素向前移动一个单位,并把最后一位的值置空,方便GC LinkedList实现原理要点概括 参考文献: 1.http://www.cnblogs.com/ITtangtang/p/3948610.htmll 2

hashmap和hash算法研究

試著忘記壹切 提交于 2019-12-10 00:00:23
  总述   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 中是这样做的:调用

HashMap源码分析

牧云@^-^@ 提交于 2019-12-09 23:40:15
一、源码分析 1.构造方法源码: static final int DEFAULT_INITIAL_CAPACITY = 16; static final int MAXIMUM_CAPACITY = 1 << 30; static final float DEFAULT_LOAD_FACTOR = 0.75f; transient Entry[] table; transient int size; int threshold; final float loadFactor; transient int modCount; public HashMap(int initialCapacity, float loadFactor) { if (initialCapacity < 0) throw new IllegalArgumentException("Illegal initial capacity: " + initialCapacity); if (initialCapacity > MAXIMUM_CAPACITY) initialCapacity = MAXIMUM_CAPACITY; if (loadFactor <= 0 || Float.isNaN(loadFactor)) throw new IllegalArgumentException("Illegal

理解 HashMap 加载因子 loadFactor

有些话、适合烂在心里 提交于 2019-12-09 23:39:45
一、何为加载因子? 加载因子是表示Hsah表中元素的填满的程度.若:加载因子越大,填满的元素越多,好处是,空间利用率高了,但: 冲突的机会 加大了.反之,加载因子越小,填满的元素越少,好处是: 冲突的机会减小了 ,但:空间浪费多了. 冲突的机会越大,则查找的成本越高.反之,查找的成本越小.因而,查找时间就越小. 因此,必须在 "冲突的机会"与"空间利用率"之间寻找一种平衡与折衷. 这种平衡与折衷本质上是数据结构中有名的" 时-空 "矛盾的 平衡与折衷 . 二、HashMap中的加载因子 HashMap默认的加载因子是0.75,最大容量是16,因此可以得出HashMap的默认容量是:0.75*16=12。 用户可以自定义最大容量和加载因子。 HashMap 包含如下几个构造器: HashMap():构建一个初始容量为 16,负载因子为 0.75 的 HashMap。 HashMap(int initialCapacity):构建一个初始容量为 initialCapacity,负载因子为 0.75 的 HashMap。 HashMap(int initialCapacity, float loadFactor):以指定初始容量、指定的负载因子创建一个 HashMap。 来源: oschina 链接: https://my.oschina.net/u/2244961/blog

Mybatis ResultMap is HashMap<String,Object>

我们两清 提交于 2019-12-09 23:05:58
问题 I can't seem to find a way to get the result map as a map My sql is <select id="retrievePackageHeader" parameterType="java.lang.String" resultType="PackageHeaderMap"> SELECT CONCAT(SCE_WRK_CTRL_NB, AC_CRR_CDE) as row_id, MTC_CHK_TYP_CDE, PLNR_REVW_IND, PLNR_OWD_IND, PKG_SLOT_TYP_CDE FROM WSM_PKG_HDR WHERE AC_NB = '${value}'; WITH UR </select> Now i need row_id as the map (key) and the other columns as attributes of a bean. I want to do something like my code below, but I can't find the

How to sort HashMap<String, String[]> by String in android

流过昼夜 提交于 2019-12-09 23:00:27
问题 I have this code: HashMap<String, String[]> unsorted = new HashMap<String, String[]>(); String[] values = new String[3]; String key; //add data to hashmap key = "abc"; values[0] = "a"; values[1]="b"; values[2]="c"; unsorted.put(key, values); key = "abc"; values[0] = "aa"; values[1]="bb"; values[2]="cb"; unsorted.put(key, values); key = "def"; values[0] = "d"; values[1]="e"; values[2]="f"; unsorted.put(key, values); //sort hashmap /***********/ //output should be: { abc-[a,b,c], abc-[aa,bb,cc]

Clojure hash-map to xml

放肆的年华 提交于 2019-12-09 21:00:44
问题 I am trying to convert the following maps to xml (any key with a vector value needs to repeat the key in the xml for each element in the vector) (use 'clojure.xml) (defn map-to-xml2 [k v] (cond (nil? k) (for [[e a] v] {:tag e :content (map-to-xml2 e a)}) (map? v) (for [[e a] v] {:tag e :content (map-to-xml2 e a)}) (vector? v) (for [x v] {:tag k :content (for [[e a] x] {:tag e :content (map-to-xml2 e a)})}) :else [(str v)])) (def studios [{:company {:name "Acme Corp" :id 1 :rank 20 :employee [

Different fields for equals and hashcode

蹲街弑〆低调 提交于 2019-12-09 20:46:02
问题 I agree with the statement from this post What issues should be considered when overriding equals and hashCode in Java? Use the same set of fields that you use to compute equals() to compute hashCode(). But i've some doubts : Is this absolutely necessary to have same fields ? If yes, what if I don't use same field ? Will it affect HashMap performance or HashMap Accuracy ? 回答1: The fields don't have to be the same. The requirement is for two objects that are equal, they must have the same hash

how does weakhashmap work? [duplicate]

余生颓废 提交于 2019-12-09 17:02:44
问题 This question already has answers here : When would you use a WeakHashMap or a WeakReference? (10 answers) Closed 4 years ago . As in how does weakhashmap understand that a reference to one of its key is obsolete now especially if the key is a String which is pooled? 回答1: You must not use String literals with WeakHashMap (well you can but there would be no point in it): String myKey = "somekey"; instead you must use: String myKey = new String("somekey"); In the latter case String is not

what must be hashcode of null objects in Java?

时光总嘲笑我的痴心妄想 提交于 2019-12-09 15:51:59
问题 According to a comment from this post, hascode of null objects can throw NPE or a value of zero . This is implementation specific. but within the same implementation, why does Objects.hashcode and hascode(instance) return different values. for ex: public class EqualsTesting { public static void main(String[] args){ String p1 =null; String p2 = null; System.out.println(Objects.hashCode(p1)); System.out.println(p2.hashCode()); } } Output: 0 Exception in thread "main" java.lang