1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
Some JDK APIs are obsolete and have preferred alternatives.
## `LinkedList`
`LinkedList` almost never out-performs `ArrayList` or `ArrayDeque`[^1]. If you
are using `LinkedList` as a list, prefer `ArrayList`. If you are using
`LinkedList` as a stack or queue/deque, prefer `ArrayDeque`.
Migration gotcha: `LinkedList` permits `null` elements; `ArrayDeque` rejects
them. The documentation for `Deque` strongly discourages users from inserting
`null`, even into implementations that permit it. So, if you are using a
`LinkedList` for this purpose, you should likely stop, and you will _need_ to
stop in order to migrate to `ArrayDeque`.
## `Vector`
`Vector` performs synchronization that is usually unnecessary; prefer
`ArrayList`.
If a synchronized collection is necessary, use `Collections.synchronizedList` or
a data structure from `java.util.concurrent`.
## `Hashtable` and `Dictionary`
This is a nonstandard class that predates the Java Collections Framework; prefer
`LinkedHashMap` or `HashMap`.
If synchronization is necessary, `java.util.concurrent.ConcurrentHashMap` is
usually a good choice.
## `java.util.Stack`
`Stack` is a nonstandard class that predates the Java Collections Framework;
prefer `ArrayDeque`.
If a synchronized collection is necessary, use a data structure from
`java.util.concurrent`.
When migrating from `Stack` to `Deque`, note that the `Stack` methods
`push`/`pop`/`peek`/`add`/`iterator` correspond to the `Deque` methods
`addFirst`/`removeFirst`/`peekFirst`/`addFirst`/`descendingIterator`.
## `StringBuffer`
`StringBuffer` performs synchronization that is rarely necessary and has
significant performance overhead. Prefer `StringBuilder`, which does not do
synchronization.
If synchronization is necessary, consider creating an explicit lock object and
using `synchronized` blocks.
## `Enumeration`
An ancient precursor to `Iterator`.
## `SortedSet` and `SortedMap`
Replaced by `NavigableSet` and `NavigableMap` in Java 6.
[^1]: People generally choose `LinkedList` because they want fast insertion and
removal. However, `LinkedList` has slow traversal, and typically you need
to traverse the list to find the place to insert/remove. It turns out that
the cost of traversing to a location in a `LinkedList` is approximately 4x
the cost of copying an element in an `ArrayList`. Thus, `LinkedList`'s
traversal cost dominates and results in poorer performance. More info:
https://stuartmarks.wordpress.com/2015/12/18/some-java-list-benchmarks/
|