JAVA : Collections

java.util.Collections

List Interface

A List cares about the index. All three List implementations are
ordered by index position – a position that you determine either by
setting an object at a specific index or by adding it without
specifying position, in which case the object is added to the end.

ArrayList

  • Ordered, by index
  • Unsorted
  • Fast iteration, but not fast insertion and deletion
  • Implements RandomAccess marker interface – supports
    fast (generally constant time) random access

Vector

  • Ordered, by index
  • Unsorted
  • Methods are synchronized
  • Implements RandomAccess marker interface – supports
    fast (generally constant time) random access
  • One of the the Original collections

LinkedList

  • Ordered, by index
  • Unsorted
  • Elements are doubly-linked
  • Fast insertion and deletion, not fast iteration
  • Good for implementing stacks and queues, supports adding or
    removing from beginning or end



Set Interface

A Set does not allow duplicates, it uses equals() to determine
the uniqueness

HashSet

  • Unordered
  • Unsorted
  • Uses hashCode() of the object being inserted, why?

LinkedHashSet

  • Ordered, by insertion order
  • Elements are doubly-linked
  • Use it when you care about the insertion order

TreeSet

  • Sorted, by natural order or custom implementation
  • Implements NavigableSet



Map Interface

It is about key/value pairs, keys should be unique. Relies on equals() to determine the keys’ uniqueness

HashMap

  • Unordered
  • Unsorted
  • Allows one null key and multiple null values in a collection
  • Uses hashCode(), why?
  • Faster insertion and deletion, not fast iteration

Hashtable

  • Unordered
  • Unsorted
  • Methods are synchronized
  • Does not allow null

LinkedHashMap

  • Ordered, by insertion order
  • Unsorted
  • Faster insertion, not fast insertion and deletion
  • Doubly-linked?

TreeMap

  • Sorted, by natural order or custom implementation



Queue Interface

  • Typically FIFO, but other orders are possible.
  • Priority

    • Sorted, by to-do order
    • priority-in, priority-out
    Advertisements

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s

    %d bloggers like this: