The Collection interface serves as the foundational interface in Java's collection framework. As a subinterface of Iterable, it establishes the core contract for all collection types, though it cannot be instantiated directly. This design reflects Java's object-oriented principles for managing groups of objects efficiently.
</div>The Iterable inheritance enables all collections to support iteration through iterators and enhanced for-loops. This architectural decision promotes consistency across different collection implementations while maintaining flexibility in how iteration is implemented internally.
Primary Collection Subtypes
---------------------------
### List Interface and Implementations
The List interface maintains element ordering and allows duplicates with indexed access:
<div>```
List<String> arrayList = new ArrayList<>();
List<String> linkedList = new LinkedList<>();
List<String> vector = new Vector<>();
// Key List operations
arrayList.add(0, "first");
String element = arrayList.get(1);
int position = arrayList.indexOf("target");
Set Interface Variants
Sets enforce uniqueness constraints with different underlying mechanisms:
hashSet.add("unique"); boolean exists = hashSet.contains("test");
</div>### Queue Interface Structure
Queues follow specific ordering policies like FIFO or priority-based processsing:
<div>```
Queue<String> queue = new LinkedList<>();
queue.offer("first");
queue.offer("second");
String head = queue.poll(); // Returns and removes head
String peeked = queue.peek(); // Returns head without removal
Collections store single elements while Maps maintain key-value associations:
// Map stores key-value pairs Map<String, Integer> mappings = new HashMap<>(); mappings.put("key", 100);
</div>Though Map doesn't extend Collection, some implementations internally utilize Map structures (e.g., HashSet uses HashMap).
Utility Class Collections
-------------------------
The Collections class provides static utility methods for collection manipulation:
<div>```
List<Integer> numbers = Arrays.asList(3, 1, 4, 1, 5);
Collections.sort(numbers); // Ascending order
Collections.reverse(numbers); // Reverse sequence
Collections.shuffle(numbers); // Random arrangement
int max = Collections.max(numbers); // Maximum value
Selecting Apropriate Collections
Choice depends on specific requirements:
- Duplicate handling: Lists allow duplicates, Sets enforce uniqueness
- Ordering needs: LinkedHashSet preserves insertion order, TreeSet sorts elements
- Performance priorities: ArrayList for frequent reads, LinkedList for modifications
- Thread safety: Vector and synchronized wrappers for concurrent access
Modern applications often leverage streams for functional operations:
</div>