Java Collections Framework
Java Collections Framework
The Java Collection Framework provides a structured way to manage groups of objects using a
variety of data structures. Key components include interfaces like Collection, List, Set, Queue, and
Map, and classes like ArrayList, LinkedList, HashSet, HashMap, and others. These structures allow
for efficient data manipulation, storage, and retrieval based on specific needs such as ordered,
unordered, or sorted data.
Collection Interfaces
• List: An ordered collection that allows duplicate elements. Common implementations include
ArrayList, LinkedList, Vector, and Stack.
o ArrayList: A resizable array implementation, providing fast random access but slower
insertions and deletions in the middle.
o LinkedList: A doubly-linked list, suitable for frequent insertions and deletions but
slower for random access.
• Map: A collection that maps keys to values, with no duplicate keys allowed.
o HashMap: Based on a hash table, allowing one null key and multiple null values.
Basic Operations
The Java Collection Framework provides several basic operations that can be performed on lists, sets,
and maps. These include:
• Adding Elements:
• Removing Elements:
o remove(element): Removes the specified element from the collection.
• Accessing Elements:
o For loop: Can iterate through a list using a basic for loop, using the index.
o Iterator: Provides a way to traverse elements and allows for element removal during
iteration.
Bulk Operations
• removeAll(): Removes all elements in the current collection that are present in the specified
collection.
• retainAll(): Retains only the elements in the current collection that are also in the specified
collection.
• containsAll(): Checks if the current collection contains all elements from the specified
collection.
Iteration
Iteration is accessing each element of a collection one by one. Different ways to iterate include:
• Enhanced for loop: Simple way to iterate over collections and arrays.
• Iterating over a Map: Uses entrySet(), keySet(), or values() to iterate over key-value pairs,
keys, or values, respectively.
Lists
Lists are ordered collections that allow duplicate elements.
• Basic Operations:
o Adding: add(E element) adds to the end, add(int index, E element) adds at a specific
index.
o Checking for Element: contains(Object o) returns true if the list contains the
element.
• Iteration: Using for loop, enhanced for loop, iterator, and forEach.
ArrayList vs LinkedList
The primary difference between ArrayList and LinkedList is their underlying data structure and
performance characteristics.
• ArrayList:
o Simple implementation.
o Faster iteration.
• LinkedList:
o Slower iteration.
Sets
Sets are collections that do not allow duplicate elements.
• Basic Operations:
o Checking for Elements: contains(Object o) returns true if set contains the specified
element.
• Set Operations: union, intersection, difference using addAll(), retainAll(), and removeAll()
respectively.
Maps
Maps are collections of key-value pairs. Each key is associated with one value, and duplicate keys are
not allowed.
• Basic Operations:
o Adding: put(K key, V value) adds a key-value pair, updating the value if the key exists.
o Accessing: get(Object key) retrieves the value associated with the key.
o Removing: remove(Object key) removes the key-value pair associated with the key.
The main differences lie in their underlying data structures, element order, and performance.
• HashMap:
o No guaranteed order.
• LinkedHashMap:
• TreeMap:
• Synchronization: None of these maps are synchronized. For thread safety, you can use
Collections.synchronizedMap() or ConcurrentHashMap.
Generics in Java
Generics allow you to write code that can operate on objects of various types while providing
compile-time type safety, reducing runtime errors and eliminating the need for casting.
• Bounded Types: You can restrict the types used as arguments in a generic class or method
with the extends keyword.
• Generic Types:
o Parameterized types enhance type safety, improve code reusability and readability.
o Unbounded Wildcard: Use <?> when the exact type is unknown or irrelevant.
o Upper Bounded Wildcard: Use <? extends T> when reading from a structure.
o Lower Bounded Wildcard: Use <? super T> when adding elements to a structure.
Lambda Expressions
Lambda expressions are anonymous functions that enable more concise code, especially with
functional interfaces and the Java Streams API.
• Lambda Type Inference: The compiler can automatically deduce the types of parameters or
return values in a lambda expression based on the functional interface.
• Lambda Parameters:
o Block Body: Multiple statements enclosed in curly braces, with a return statement if
needed.
o Returning Values: Implicit return for single expression; explicit return for block
bodies.
• Lambdas as Objects: Lambdas are treated as instances of functional interfaces and can be
assigned to variables, passed as arguments, or returned from methods.
This overview covers the major aspects of the Java Collection Framework, including data structures,
operations, generics, and lambda expressions, as described in the sources.