Java Collections Framework: Collection Hierarchy and Interfaces - Quiz

Total: 7 questions

1. In which version of Java did the Collections Framework appear?

The Java Collections Framework appeared in JDK 1.2 (J2SE 1.2, 1998). Before that, Java had only Vector, Stack and Hashtable from Java 1.0; they were fitted into the new hierarchy later. Generics were added to collections in Java 5.

2. In which meanings is the word "collection" used in Java?

- a data structure in which objects are stored

- the java.util.Collection interface

- a class that holds static utility methods for collections (java.util.Collections)

3. 

What are the four basic interfaces of the Java Collections Framework?

The four basic interfaces are List, Set, Queue and Map. List, Set and Queue extend Collection (which extends Iterable); Map stores key–value pairs and is a separate branch that does not extend Collection.

Counting the sub-interfaces, the framework has six core interfaces: List, Set, SortedSet/NavigableSet, Queue, Deque and Map.

4. 

What parts does the Java Collections Framework consist of?

The Java Collections Framework in the java.util package has three parts:

  • Interfaces describe what a collection can do: Collection, List, Set, Queue, Deque, Map.
  • Implementations are classes with a concrete data structure inside: ArrayList, LinkedList, HashSet, TreeSet, ArrayDeque, HashMap.
  • Algorithms are static methods of the Collections and Arrays utility classes: sort(), binarySearch(), shuffle(), max().

A variable is usually declared with the interface type: List<String> names = new ArrayList<>();. Then the implementation can be replaced by changing one line.

5. 

How do Arrays.asList(), Collections.unmodifiableList(), List.of() and List.copyOf() differ?

  • Arrays.asList(...) returns a fixed-size list backed by the array: set() works and changes the array, while add() and remove() throw UnsupportedOperationException. Null elements are allowed.
  • Collections.unmodifiableList(list) is a read-only view: every modifying method throws UnsupportedOperationException, but changes to the original list show through the view.
  • List.of(...) (Java 9) creates a truly immutable list with no link to any source; null elements are not allowed (NullPointerException, even for contains(null)).
  • List.copyOf(collection) (Java 10) makes an immutable copy, so later changes to the source are not visible; null is not allowed either.

For a mutable list with initial elements, wrap the factory: new ArrayList<>(List.of("a", "b")).

6. 

Which thread-safe collections does Java offer instead of ArrayList and HashMap?

ArrayList, HashSet and HashMap are not thread-safe: concurrent writes lose data. There are three groups of alternatives:

  • Legacy synchronized classes Vector and Hashtable – only for old code.
  • Wrappers Collections.synchronizedList() and Collections.synchronizedMap() with one lock for all methods; iteration must be wrapped in synchronized (list) { ... } manually.
  • Classes of the java.util.concurrent package (Java 5): ConcurrentHashMap (the default choice for a shared map, null keys and values are not allowed), CopyOnWriteArrayList (many reads, rare writes), ConcurrentLinkedQueue, BlockingQueue implementations for producer–consumer, ConcurrentSkipListMap/ConcurrentSkipListSet instead of TreeMap/TreeSet.
7. 

What is SequencedCollection in Java 21?

SequencedCollection is an interface added in Java 21 (JEP 431) together with SequencedSet and SequencedMap. It describes collections with a defined encounter order and gives them common methods getFirst(), getLast(), addFirst(), addLast(), removeFirst(), removeLast() and reversed().

List, Deque, SortedSet and SortedMap extend these interfaces, and LinkedHashSet and LinkedHashMap implement them. So on Java 21 a plain ArrayList has list.getFirst(); on Java 17 this call does not compile and you write list.get(0).

Page 1 of 1