Sobes.tech
Junior

Tell us about the collection hierarchy in Java.

sobes.tech AI

Answer from AI

The hierarchy of collections in Java is built around two root interfaces: Collection and Map. It is defined in the java.util package.

  • Collection<E>: The root interface for most collections. Represents a group of elements. Does not guarantee order or uniqueness of elements.
    • List<E>: An ordered collection (sequence). Elements have a specific index. Can contain duplicates.
      • ArrayList<E>: Array-based implementation. Fast random access, slow inserts/deletes in the middle.
      • LinkedList<E>: Linked list implementation. Slow random access, fast inserts/deletes at beginning/end.
      • Vector<E>: Deprecated synchronized class. Similar to ArrayList, but thread-safe (and less performant).
      • Stack<E>: Deprecated class extending Vector, implements stack behavior (LIFO).
    • Set<E>: A collection containing unique elements.
      • HashSet<E>: Hash table-based implementation. Unordered. Fast add, remove, contains operations.
      • LinkedHashSet<E>: Maintains insertion order.
      • SortedSet<E>: A set sorted in ascending order.
        • TreeSet<E>: Red-black tree implementation. Guarantees sorted order.
    • Queue<E>: A collection designed for holding elements prior to processing, often in FIFO order.
      • PriorityQueue<E>: Priority queue implementation. Elements are retrieved according to their priority.
      • Deque<E>: (Double Ended Queue) A double-ended queue. Supports insertion and removal from both ends.
        • ArrayDeque<E>: Array-based implementation of Deque. Faster than LinkedList for stack or queue use.
  • Map<K, V>: An object that maps keys to values. Keys are unique, values can repeat.
    • HashMap<K, V>: Hash table-based implementation. Unordered. Fast put, get, remove.
    • LinkedHashMap<K, V>: Maintains insertion or access order.
    • SortedMap<K, V>: A map sorted by keys.
      • TreeMap<K, V>: Red-black tree implementation. Guarantees sorted order by keys.
    • Hashtable<K, V>: Deprecated synchronized class. Similar to HashMap, but thread-safe (and less performant).

Main interfaces:

Interface Description Order Uniqueness Duplicate values
Collection Group of elements +/- +/- Yes
List Ordered collection Yes No Yes
Set Collection of unique elements +/- Yes No
Queue Collection for processing (FIFO) Yes No Yes
Deque Double-ended queue Yes No Yes
Map Key-value pairs +/- By key Yes
SortedSet Sorted set Yes Yes No
SortedMap Map sorted by keys Yes By key Yes

Usage examples:

// Example of using ArrayList
List<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
System.out.println(names.get(0)); // Alice

// Example of using HashSet
Set<Integer> uniqueNumbers = new HashSet<>();
uniqueNumbers.add(1);
uniqueNumbers.add(2);
uniqueNumbers.add(1); // Duplicate is ignored
System.out.println(uniqueNumbers.size()); // 2

// Example of using HashMap
Map<String, Integer> ages = new HashMap<>();
ages.put("Alice", 30);
ages.put("Bob", 25);
System.out.println(ages.get("Alice")); // 30
Tell us about the collection hierarchy in Java. — Java - sobes.tech