关键词:Java 1.6 集合,Java 1.7集合,Java集合引导,概览

Java collections overviewall JDK 1.6/1.7 standard collections are described and categorized in this overview.

Tags: Java 1.6 collectionsJava 1.7 collectionsJava collections guideoverview.

Here is a very brief summary of all JDK collections:

 Single threadedConcurrentLists

  • ArrayList - generic array-based
  • LinkedList - do not use
  • Vector - deprecated
  • CopyOnWriteArrayList - seldom updated, often traversed

Queues / deques

  • ArrayDeque - generic array-based
  • Stack - deprecated
  • PriorityQueue - sorted retrieval operations
  • ArrayBlockingQueue - bounded blocking queue
  • ConcurrentLinkedDeque / ConcurrentLinkedQueue - unbounded linked queue (CAS)
  • DelayQueue - queue with delays on each element
  • LinkedBlockingDeque / LinkedBlockingQueue - optionally bounded linked queue (locks)
  • LinkedTransferQueue - may transfer elements w/o storing
  • PriorityBlockingQueue - concurrent PriorityQueue
  • SynchronousQueue - Exchanger with Queue interface

Maps

  • HashMap - generic map
  • EnumMap - enum keys
  • Hashtable - deprecated
  • IdentityHashMap - keys compared with ==
  • LinkedHashMap - keeps insertion order
  • TreeMap - sorted keys
  • WeakHashMap - useful for caches
  • ConcurrentHashMap - generic concurrent map
  • ConcurrentSkipListMap - sorted concurrent map

Sets

  • HashSet - generic set
  • EnumSet - set of enums
  • BitSet - set of bits/dense integers
  • LinkedHashSet - keeps insertion order
  • TreeSet - sorted set
  • ConcurrentSkipListSet - sorted concurrent set
  • CopyOnWriteArraySet - seldom updated, often traversed

下面是JDK所有集合的一个简单总结(附加说明是指在什么场景下适合使用):

 

单线程

并发

Lists

  • ArrayList - 通用的范型数组
  • LinkedList - 不要使用
  • Vector - 已弃用
  • CopyOnWriteArrayList - 很少修改, 经常遍历

Queues / deques

  • ArrayDeque - 通用的范型数组
  • Stack - 已弃用
  • PriorityQueue - 已序的检索操作
  • ArrayBlockingQueue - 有界阻塞队列
  • ConcurrentLinkedDeque / ConcurrentLinkedQueue - unbounded linked queue (CAS)
  • DelayQueue - queue with delays on each element
  • LinkedBlockingDeque / LinkedBlockingQueue - optionally bounded linked queue (locks)
  • LinkedTransferQueue - may transfer elements w/o storing
  • PriorityBlockingQueue - 有优先级的并发队列
  • SynchronousQueue - 有队列接口的交换器

Maps

  • HashMap - 通用map
  • EnumMap - 枚举类型的键值
  • Hashtable - 已弃用
  • IdentityHashMap - 使用 == 操作符对key进行比较
  • LinkedHashMap - 保持插入的顺序
  • TreeMap - key是已序的
  • WeakHashMap - 对caches很有用
  • ConcurrentHashMap - 通用并发map
  • ConcurrentSkipListMap - 已序的并发 map

Sets

  • HashSet - 通用 set
  • EnumSet - 枚举类型的set
  • BitSet - 存bits的set
  • LinkedHashSet - 保持插入的顺序
  • TreeSet - 已序的 set
  • ConcurrentSkipListSet - 已序的可处理并发的 set
  • CopyOnWriteArraySet - 很少修改, 经常遍历

 

java.util.ArrayList
关键词:低时延,高吞吐,CPU缓存易命中,Java集合,CPU优化,内存优化

java.util.ArrayList performance guidejava.util.ArrayList:

Tags: low latencyhigh throughputCPU cache friendlyJava collectionsCPU optimizationmemory optimization.

Try to follow these rules while using ArrayList:

  • Add elements to the end of the list
  • Remove elements from the end too
  • Avoid containsindexOf and remove(Object) methods
  • Even more avoid removeAll and retainAll 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 performancejava.util.LinkedListjava.util.ArrayDeque:

Tags: Java collectionsCPU optimizationavoid 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的代码跑得快,请坚持下面这些规则:
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. 注意一个计算过标识散列码的对象不能被偏向锁使用。