Java Collections

·

1 min read

List

  • ArrayList

    • It internally uses an array

    • Fast to access the elements by index

    • Slower if you want to do operations in the middle

  • LinkedList

    • Based on a Double Linked

    • Very fast for deleting or adding elements in the middle

  • Vector

    • It belongs to the legacy

    • Synchronized

  • Stack

    • It basically implements a stack.

Queue

  • PrioritQueue

    • Implements a Queue
  • ArrayDeque

    • Implements a Queue, but allows us to do operations at the bottom and at the top

Set

  • HashSet

    • Doesn't allow duplications

    • Doesn't keep the order of insertion

  • LinkedHashSet(Log(1))

    • Keep the order of insertion
  • TreeSet(Log(n))

    • It sorts the elements using the natural order

Map

  • HashMap

    • Unsorted keys

    • A pair of keys and values.

    • Allows null values at the key

  • LinkedHashMap

    • Maintain the insertion order
  • HashTable

    • The same as HashMap, but it is synchronized

    • Slower than HashMap

    • Belongs to the legacy of Java. Try to not use this

  • TreeMap

    • The key is sorted using the natural order of the Keys.

    • Also, you can implement your own sort order