Interview Q&A · Java · 5 questions in this topic
The Collections Framework: collection, Collection, Collections — Java Interview Q&A
The big picture of the Collections Framework: what it is, what a collection is, and how collection, Collection and Collections differ. Part of Collections Framework · 294 questions in the Java bank.
What is the Collections Framework?
Answer
The Collections Framework is a unified architecture in java.util for storing and manipulating groups of objects. It consists of core interfaces such as Collection, List, Set, Queue, Deque and Map, concrete implementations such as ArrayList, HashSet and HashMap, and reusable algorithms such as sorting and searching in the Collections utility class. Using it gives code reuse, tuned performance, and interoperability because all implementations share the same interface vocabulary. Modern code uses it with generics so collections are type safe at compile time.
Key points
- 1Interfaces define behaviour, implementations supply data structures, and algorithms operate on interfaces.
- 2Most types live in java.util, with concurrent variants in java.util.concurrent.
- 3It reduces effort because you reuse tested structures instead of writing your own.
What is a collection?
Answer
A collection is a container object that manages a group of related objects as a single unit. Typical examples are a list of strings or a set of integers held in one object rather than in separate variables. It supports basic operations such as adding objects, removing them, retrieving them, and iterating over them. In everyday speech collection means this general idea, while Collection with a capital C means the root interface described next.
collection vs Collection vs Collections?
Answer
Lowercase collection is the general concept of a group of objects managed as one unit. Capital C Collection is the root interface in java.util that declares the common operations every collection supports. Capital C Collections with an s is a final utility class containing only static methods such as sort, binarySearch, reverse, shuffle, and wrappers for unmodifiable or synchronized views. Interviews test this spelling difference because the three names sound identical but mean concept, interface, and utility class.
💡 Say the three meanings in order: concept, root interface, static utility class.
What does the Collection interface provide?
Answer
Collection is the root interface for List, Set and Queue, and it extends Iterable so every collection works with for-each loops. It declares the core operations add, remove, size, isEmpty, contains, iterator, and toArray for moving elements to an array. It also declares bulk operations addAll, removeAll, retainAll, and containsAll that operate on whole groups at once. Concrete classes implement these methods to suit their own storage strategy, such as indexed storage for lists or hashed storage for sets.
Key points
- 1Extends Iterable, so iterator and for-each traversal come free.
- 2Bulk methods let you union, subtract, or intersect collections in one call.
- 3Map does not extend Collection because it stores key-value pairs, not single elements.
Which interfaces extend Collection?
Answer
Directly or indirectly, List, Set, and Queue extend Collection, and Deque extends Queue since Java 6 so it is also part of the family. List adds positional access, Set adds uniqueness, and Queue and Deque add ordering for processing such as FIFO or LIFO use. Map does not extend Collection because it maps keys to values rather than holding single elements. Factory methods such as List.of, Set.of, and Map.of create compact immutable instances of these families since Java 9.
Learn this topic
Recommended resourcesAffiliate links · may earn us a commission