Sobes.tech
Junior

What collections exist in Java?

sobes.tech AI

Answer from AI

The Java Collections Framework provides a variety of collections for storing and managing data. The main interfaces and their implementations:

  • Collection: the root interface.
    • List: an ordered collection that allows duplicates.
      • ArrayList: based on an array, quick access by index, slow insertion/deletion in the middle.
      • LinkedList: based on a doubly linked list, quick insertion/deletion, slow access by index.
      • Vector: thread-safe version of ArrayList, deprecated.
      • Stack: based on Vector, LIFO principle.
    • Set: a collection of unique elements, without a specific order (except SortedSet).
      • HashSet: based on a hash table, quick access and insertion/deletion.
      • LinkedHashSet: maintains insertion order.
      • TreeSet: stores elements in sorted order.
    • Queue: a collection of elements, often used for storing elements before processing in FIFO order.
      • PriorityQueue: elements are retrieved in order of their priority.
      • ArrayDeque: a double-ended queue.
    • Deque: a double-ended queue, elements can be added and removed from both ends.
  • Map: stores key-value pairs, keys are unique.
    • HashMap: based on a hash table, quick access by key.
    • LinkedHashMap: maintains insertion or access order.
    • TreeMap: stores pairs in sorted order by keys.
    • Hashtable: thread-safe version of HashMap, deprecated.

Thread-safe collections:

  • Collections.synchronized*: wrappers around existing collections.
  • Concurrent*: package java.util.concurrent, collections with improved performance in concurrent environments.
    • ConcurrentHashMap
    • CopyOnWriteArrayList
    • ConcurrentLinkedQueue

And other less common collections.

What collections exist in Java? — Android - sobes.tech