java collections

List

  1. ArrayList

    • permit null.
    • not synchronized.

      List list = Collections.synchronizedList(new ArrayList(...))

    • an application can increase the capacity of an ArrayList instance before adding a large number of elements using the ensureCapacity operation.

      ensureCapacity(int minCapacity)

  2. Vector

    • synchronized.
  3. Stack

    • extends class Vector.
    • a more complete and consistent set of LIFO stack operations is provided by the Deque interface and its implementations.
  4. LinkedList

    see also Queue.

Map

  1. Hashtable

    • the keys must implement the hashCode method and the equals method.
    • only non-null object can be used as a key or as a value.
    • synchronized.

      Hashtable(int initialCapacity, float loadFactor=.75)

  2. HashMap

    • permits null values and the null key.
    • not synchronized.

      Map m = Collections.synchronizedMap(new HashMap(...))

  3. LinkedHashMap

    • permits null values and the null key.
    • maintains a doubly-linked list running through all of its entries which defines the iteration ordering (insertion-order or access order which its entries were last accessed, from least-recently accessed to most-recently. This is well-suited to building LRU caches).

      LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder)

    • the removeEldestEntry(Map.Entry) method may be overridden to impose a policy for removing stale mappings automatically when new mappings are added to the map.

      1
      2
      3
      4
      private static final int MAX_ENTRIES = 100;
      protected boolean (Map.Entry eldest) {
      return size() > MAX_ENTRIES;
      }
    • not synchronized.

  4. TreeMap

    • the implementation based on Red-Black tree.
    • permits null values.
    • sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time.
    • the ordering maintained by a sorted map must be consistent with equals if this sorted map is to correctly implement the Map interface.
    • not synchronized.

      SortedMap m = Collections.synchronizedSortedMap(new TreeMap(...))

  5. ConcurrentHashMap

Queue

  1. Queue

    each of these methods exists in two forms: one throws an exception if the operation fails, the other returns a special value (null or false).

    Throws exception Returns special value
    Insert add(e) offer(e)
    Remove remove() poll()
    Examine element() peek()
    • Queue implementations generally do not allow insertion of null elements, although some implementations, such as LinkedList, but it should not be inserted into a Queue, as null is also used as a special return value by the poll method to indicate that the queue contains no elements.
  2. PriorityQueue

    • based on a priority heap. Ordered according to their natural ordering, or by a Comparator provided at queue construction time.

      public PriorityQueue(Comparator<? super E> comparator)

    • not synchronized.
  3. Deque

    each of these methods exists in two forms: one throws an exception if the operation fails, the other returns a special value (null or false).

    Head Tail
    Throws exception Returns special value Throws exception Returns special value
    Insert addFirst(e) offerFirst(e) addLast(e) offerLast(e)
    Remove removeFirst() pollFirst() removeLast() pollLast()
    Examine getFirst() peekFirst() getLast() peekLast()
    • this interface provides two methods to remove interior elements, removeFirstOccurrence and removeLastOccurrence.
  4. ArrayDeque

    • resizable-array implementation of the Deque interface.
    • null elements are prohibited.
    • not thread-safe.
    • faster than Stack when used as a stack, and faster than LinkedList when used as a queue.
  5. LinkedList

    • doubly-linked list implementation of the List and Deque interfaces.
    • permits all elements (including null).
    • not synchronized.

      List list = Collections.synchronizedList(new LinkedList(...))

  6. BlockingQueue

    the third method blocks the current thread indefinitely until the operation can succeed, and the fourth blocks for only a given maximum time limit before giving up.

    Throws exception Returns special value Blocks Times out
    Insert add(e) offer(e) put(e) offer(e, time, unit)
    Remove remove() poll() take() poll(time, unit)
    Examine element() peek() not applicable not applicable
    • does not accept null elements.
    • thread-safe.
    • designed to be used primarily for producer-consumer queues.