Java 7 对ArrayList和HashMap的性能的提升
2019独角兽企业重金招聘Python工程师标准>>>
在最新的Java7的ArrayList和HashMap, JDK做了一些调整.
ArrayList以前的版本是默认创建10个元素, HashMap默认是创建16个,但是在Java7之后,如果默认的构造函数ArrayList是相当于创建是new ArrayList(0), HashMap是在调用put时再分配空间的.
这样做不仅节省了内存,同时垃圾回收也节省了时间.
old ArrayList
/*** Constructs an empty list with the specified initial capacity.** @param initialCapacity the initial capacity of the list* @throws IllegalArgumentException if the specified initial capacity* is negative*/public ArrayList(int initialCapacity) {super();if (initialCapacity < 0)throw new IllegalArgumentException("Illegal Capacity: "+initialCapacity);this.elementData = new Object[initialCapacity];}/*** Constructs an empty list with an initial capacity of ten.*/public ArrayList() {this(10);}
new ArrayList
/*** Constructs an empty list with the specified initial capacity.** @param initialCapacity the initial capacity of the list* @throws IllegalArgumentException if the specified initial capacity* is negative*/public ArrayList(int initialCapacity) {super();if (initialCapacity < 0)throw new IllegalArgumentException("Illegal Capacity: "+initialCapacity);this.elementData = new Object[initialCapacity];}/*** Constructs an empty list with an initial capacity of ten.*/public ArrayList() {super();this.elementData = EMPTY_ELEMENTDATA;}
old HashMap
/*** Constructs an empty <tt>HashMap</tt> with the default initial capacity* (16) and the default load factor (0.75).*/public HashMap() {this.loadFactor = DEFAULT_LOAD_FACTOR;threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);table = new Entry[DEFAULT_INITIAL_CAPACITY];init();}
new HashMap put methond
/*** Associates the specified value with the specified key in this map.* If the map previously contained a mapping for the key, the old* value is replaced.** @param key key with which the specified value is to be associated* @param value value to be associated with the specified key* @return the previous value associated with <tt>key</tt>, or* <tt>null</tt> if there was no mapping for <tt>key</tt>.* (A <tt>null</tt> return can also indicate that the map* previously associated <tt>null</tt> with <tt>key</tt>.)*/public V put(K key, V value) {if (table == EMPTY_TABLE) {inflateTable(threshold);}if (key == null)return putForNullKey(value);int hash = hash(key);int i = indexFor(hash, table.length);for (Entry<K,V> e = table[i]; e != null; e = e.next) {Object k;if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {V oldValue = e.value;e.value = value;e.recordAccess(this);return oldValue;}}modCount++;addEntry(hash, key, value, i);return null;}
/*** Inflates the table.*/private void inflateTable(int toSize) {// Find a power of 2 >= toSizeint capacity = roundUpToPowerOf2(toSize);threshold = (int) Math.min(capacity * loadFactor, MAXIMUM_CAPACITY + 1);table = new Entry[capacity];initHashSeedAsNeeded(capacity);}
转载于:https://my.oschina.net/u/138995/blog/295402
Java 7 对ArrayList和HashMap的性能的提升相关推荐
- Java中使用ArrayList的10个示例–教程
Java中的ArrayList是HashMap之后最常用的集合类. Java ArrayList表示一个可自动调整大小的数组,并用于代替数组. 由于创建数组后我们无法修改数组的大小,因此我们更喜欢在J ...
- java map套arraylist,在Java中的HashMap和ArrayList的区别?
In Java, ArrayList and HashMap are used as collections. But I couldn't understand in which situation ...
- 【java学习】Arraylist和LinkedList使用场景与性能对比
介绍 ArrayList LinkedList 使用场景对比 两个实例: 二分查找 插入元素 总结 介绍 List 的三个子类的特点: ArrayList 底层结构是数组,底层查询快,增删慢. Lin ...
- Java数据结构和算法:HashMap的实现原理
HashMap源码理解 Java集合之HashMap HashMap原理及实现学习总结 HashMap源码分析 HashMap原理及实现学习总结 1. HashMap概述 HashMap是基于哈希表的 ...
- 【Java集合学习系列】HashMap实现原理及源码分析
HashMap特性 hashMap是基于哈希表的Map接口的非同步实现,继承自AbstractMap接口,实现了Map接口(HashTable跟HashMap很像,HashTable中的方法是线程安全 ...
- 深入Java集合学习系列:HashMap的实现原理
2019独角兽企业重金招聘Python工程师标准>>> HashMap概述: HashMap是基于哈希表的Map接口的非同步实现.此实现提供所有可选的映射操作,并允许使用null值和 ...
- ArrayList、HashMap、HashSet是线程不安全的,高并发下如何解决?
1.故障现象 :ArrayList的add()方法并没有使用synchronized所以是线程不安全的,会造成java.util.ConcurrentmodificationException(并发修 ...
- JAVA提高十二:HashMap深入分析
首先想说的是关于HashMap源码的分析园子里面应该有很多,并且都是分析得很不错的文章,但是我还是想写出自己的学习总结,以便加深自己的理解,因此就有了此文,另外因为小孩过来了,因此更新速度可能放缓了, ...
- 线程安全的使用ArrayList和HashMap
前言 如果你看过源码,那么你肯定知道ArrayList和HashMap是线程不安全的,它们二者都采用了fast-fail机制.但之前小组考核的时候,学长问了我如何线程安全的使用HashMap,那时候确 ...
最新文章
- AI新作品:照片迅速被画成艺术画
- java多线程解决应用挂死的问题
- 用openssl跟Gmail的smtp对话(一)
- java高并发(十一)同步容器
- 高级数据结构与算法 | LFU缓存机制(Least Frequently Used)
- 写一个函数返回参数二进制中 1 的个数(三种方法)
- ConcurrentHashMap的红黑树实现分析
- setpriority_Java Thread类的最终void setPriority(int priority)方法(带示例)
- python os write_Python 3:写入方法与os.write返回的字节数
- Jetty入门(1-1)Jetty入门教程
- centos7 怎么封装自己的镜像_在Centos7系统上制作一个7系的Docker镜像
- oracle取时间最近的一条数据_当数据库最近一直卡顿时,第一时间应该用这条sql来分析...
- 修改app的名字和图标
- HTTP并发测试工具
- 家庭影院是什么?为什么选择家庭影院?
- com.homelink.android,链家-新房二手房租房
- mysql 设置密码出现ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
- Rtmp推流adobe认证流程
- 使用API函数 GetACP 获取Windows系统当前代码页(字符编码)
- 多元函数带 Peano余项的Taylor公式的推广 (原创)