关键词:Java 1.6 集合,Java 1.7集合,Java集合引导,概览
Java collections overview: all JDK 1.6/1.7 standard collections are described and categorized in this overview.
Tags: Java 1.6 collections, Java 1.7 collections, Java collections guide, overview.
Here is a very brief summary of all JDK collections:
Single threadedConcurrentLists
ArrayList
- generic array-basedLinkedList
- do not useVector
- deprecated
CopyOnWriteArrayList
- seldom updated, often traversedQueues / deques
ArrayDeque
- generic array-basedStack
- deprecatedPriorityQueue
- sorted retrieval operations
ArrayBlockingQueue
- bounded blocking queueConcurrentLinkedDeque / ConcurrentLinkedQueue
- unbounded linked queue (CAS)DelayQueue
- queue with delays on each elementLinkedBlockingDeque / LinkedBlockingQueue
- optionally bounded linked queue (locks)LinkedTransferQueue
- may transfer elements w/o storingPriorityBlockingQueue
- concurrentPriorityQueue
SynchronousQueue
-Exchanger
withQueue
interfaceMaps
HashMap
- generic mapEnumMap
-enum
keysHashtable
- deprecatedIdentityHashMap
- keys compared with==
LinkedHashMap
- keeps insertion orderTreeMap
- sorted keysWeakHashMap
- useful for caches
ConcurrentHashMap
- generic concurrent mapConcurrentSkipListMap
- sorted concurrent mapSets
HashSet
- generic setEnumSet
- set ofenum
sBitSet
- set of bits/dense integersLinkedHashSet
- keeps insertion orderTreeSet
- sorted set
ConcurrentSkipListSet
- sorted concurrent setCopyOnWriteArraySet
- seldom updated, often traversed
下面是JDK所有集合的一个简单总结(附加说明是指在什么场景下适合使用):
| 单线程 | 并发 |
Lists |
|
|
Queues / deques |
|
|
Maps |
|
|
Sets |
|
|
java.util.ArrayList
关键词:低时延,高吞吐,CPU缓存易命中,Java集合,CPU优化,内存优化
java.util.ArrayList performance guide:
java.util.ArrayList
:Tags: low latency, high throughput, CPU cache friendly, Java collections, CPU optimization, memory optimization.
Try to follow these rules while using
ArrayList
:
- Add elements to the end of the list
- Remove elements from the end too
- Avoid
contains
,indexOf
andremove(Object)
methods- Even more avoid
removeAll
andretainAll
methods- Use
subList(int, int).clear()
idiom to quickly clean a part of the list
在使用ArrayList时请遵守以下规则:
1. 在最后面添加元素。
2. 移出元素时也从最后面移。
3. 尽量不用contains, indexOf and remove(Object)函数。
4. 更进一步,甚至不用removeAll和retainAll函数。
5. 使用subList(int, int).clear()来整块清除数据。
LinkedList,ArrayDeque
关键词:Java集合,CPU优化,避免它
java.util.LinkedList performance:
java.util.LinkedList
,java.util.ArrayDeque
:Tags: Java collections, CPU optimization, avoid it.
If you need to write fast
LinkedList
code, try to stick to these rules:
- Consider using
ArrayDeque
for queue-based algorithms- Use
ListIterator
withLinkedList
- Avoid any
LinkedList
methods which accept or return index of an element in the list - they have nothing in common with performance- Check if you have a reason to use
LinkedList.remove/removeFirst/removeLast
methods, usepollFirst/pollLast
instead- Try batch processing
LinkedList
如果要含有LinkedList的代码跑得快,请坚持下面这些规则:
1. 基于队列的算法使用ArrayDeque。
2. 用ListIterator来操作LinkedList。
3. 避免使用任何接受或者返回一个下标的LinkedList函数。
4. 反复思考你用LinkedList的理由(意思就是尽量不要用它)。用pollFirst/pollLast函数来代替remove/removeFirst/removeLast函数。
5. 处理LinkedList时,尽量一次处理多个元素。(译得不太准确)
java.util.LinkedList performance: java.util.LinkedList, java.util.ArrayDeque:
Tags: Java collections, CPU optimization, avoid it.
If you need to write fast LinkedList code, try to stick to these rules:
Consider using ArrayDeque for queue-based algorithms
Use ListIterator with LinkedList
Avoid any LinkedList methods which accept or return index of an element in the list - they have nothing in common with performance
Check if you have a reason to use LinkedList.remove/removeFirst/removeLast methods, use pollFirst/pollLast instead
Try batch processing LinkedList
LinkedList,ArrayDeque
关键词:Java集合,CPU优化,避免它
如果要含有LinkedList的代码跑得快,请坚持下面这些规则:
1. 基于队列的算法使用ArrayDeque。
2. 用ListIterator来操作LinkedList。
3. 避免使用任何接受或者返回一个下标的LinkedList函数。
4. 反复思考你用LinkedList的理由(意思就是尽量不要用它)。用pollFirst/pollLast函数来代替remove/removeFirst/removeLast函数。
5. 处理LinkedList时,尽量一次处理多个元素。(译得不太准确)
================================================================================
Bit sets: java.util.BitSet, java.util.Set<Integer>: representing set of integers in the most compact form, using bit sets to store set of Long/long values:
Tags: low latency, high throughput, CPU cache friendly, Java collections, CPU optimization, memory optimization.
Do not forget about bit sets when you need to map a large number of integer keys to boolean flags.
Sets of integer values should be replaced with bit sets in a lot of cases in order to save a lot of memory.
Bit sets:整数集最紧凑的表示形式,用bit sets存储long型集
关键词:低时延,高吞吐,CPU缓存易命中,Java集合,CPU优化,内存优化
1.当用整数来映射boolean值时不要忘了用bit sets。(翻译得不好,读原文)
2.为了节省内存,在大部分情况下都应该用bit sets代替整数型集合。
================================================================================
java.util.IdentityHashMap: discussion why an IdentityHashMap is so special and what alternatives does it have.
Tags: Java collections, object graph, avoid it.
java.util.IdentityHashMap uses System.identityHashCode to get object identity hash code. Avoid using IdentityHashMap if you either have primary key field in the objects (use them as a key for ordinary HashMap) or use Trove maps custom hashing strategy if you need to add your own equals and hashCode methods, but can't update the objects you are working on.
Do not try to iterate IdentityHashMap contents, because iteration order will be different on every run of your program, thus making your program results inconsistent.
Accessing the object identity hash code is a very cheap Java intrinsic operation.
Beware that an object with the calculated identity hash code can not be used for biased locking. While very rare in normal circumstances, you may end up in this situation if your lock will be accessed by any Java object graph traversal algorithm (serialization, for example).
IdentityHashMap:讨论为什么IdentityHashMap这么特别
关键词:Java集合,对象图,(避免使用)
1. java.util.IdentityHashMap使用System.identityHashCode来获取对象的标识散列码。如果你的对象中有用在普通HashMap的主键(primary key)字段或者你使用Trove maps来定制散列策略,那么避免使用IdentityHashMap。(没翻译完,自己看原文)
2. 因为IdentityHashMap每次迭代遍历时顺序都不一样,所以不要用迭代器遍历IdentityHashMap的内容,这会使得你的程序运行结果不一致。
3. 访问对象的标识散列码是一种成本极低的java内在操作。
4. 注意一个计算过标识散列码的对象不能被偏向锁使用。