Java Collections Framework
Core data structures and algorithms for storing and manipulating groups of objects
java (1.2+)
2025-11-03
collections
data-structures
lists
maps
sets
Description
The Java Collections Framework provides a unified architecture for representing and manipulating collections. It includes interfaces, implementations, and algorithms for common data structures.
Main Interfaces
- Collection: Root interface for all collections
- List: Ordered collection with duplicates allowed
- Set: Collection with no duplicates
- Map: Key-value pairs (not technically a Collection)
- Queue: Elements processed in FIFO order
- Deque: Double-ended queue
Common Implementations
- ArrayList: Dynamic array implementation of List
- LinkedList: Doubly-linked list implementation
- HashMap: Hash table implementation of Map
- HashSet: Hash table implementation of Set
- TreeMap: Red-black tree implementation (sorted)
- TreeSet: Red-black tree implementation (sorted)
Code
import java.util.*;public class CollectionUtils { // List operations public static <T> List<T> createList(T... elements) { return new ArrayList<>(Arrays.asList(elements)); } public static <T> void addAll(List<T> list, Collection<T> collection) { list.addAll(collection); } public static <T> boolean contains(List<T> list, T element) { return list.contains(element); } // Set operations public static <T> Set<T> createSet(T... elements) { return new HashSet<>(Arrays.asList(elements)); } public static <T> Set<T> union(Set<T> set1, Set<T> set2) { Set<T> result = new HashSet<>(set1); result.addAll(set2); return result; } public static <T> Set<T> intersection(Set<T> set1, Set<T> set2) { Set<T> result = new HashSet<>(set1); result.retainAll(set2); return result; } // Map operations public static <K, V> Map<K, V> createMap() { return new HashMap<>(); } public static <K, V> void putAll(Map<K, V> map, Map<K, V> other) { map.putAll(other); } public static <K, V> V getOrDefault(Map<K, V> map, K key, V defaultValue) { return map.getOrDefault(key, defaultValue); } // Queue operations public static <T> Queue<T> createQueue() { return new LinkedList<>(); } public static <T> void enqueue(Queue<T> queue, T element) { queue.offer(element); } public static <T> T dequeue(Queue<T> queue) { return queue.poll(); }}
// List usageList<String> names = new ArrayList<>();names.add("Alice");names.add("Bob");names.add("Charlie");// Iteratefor (String name : names) { System.out.println(name);}// Access by indexString first = names.get(0);// Set usageSet<Integer> numbers = new HashSet<>();numbers.add(1);numbers.add(2);numbers.add(1); // Duplicate ignored// Check membershipboolean contains = numbers.contains(2);// Map usageMap<String, Integer> ages = new HashMap<>();ages.put("Alice", 30);ages.put("Bob", 25);// Get valueInteger age = ages.get("Alice");// Iterate mapfor (Map.Entry<String, Integer> entry : ages.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue());}// Queue usageQueue<String> queue = new LinkedList<>();queue.offer("First");queue.offer("Second");String next = queue.poll(); // "First"// Sorted collectionsTreeSet<Integer> sorted = new TreeSet<>();sorted.add(3);sorted.add(1);sorted.add(2);// Automatically sorted: [1, 2, 3]// Collections utility methodsList<Integer> list = Arrays.asList(3, 1, 2);Collections.sort(list);Collections.reverse(list);int max = Collections.max(list);
Comments
test
a
list
abc
Please login to leave a comment.