The Java Collections Framework provides a set of classes and interfaces for storing and manipulating groups of data. Understanding collections such as List, Set, and Map is essential for effective Java programming.
For a detailed overview of the Java Collections Framework, visit our article: Java Collections Framework.
A collection in Java is a framework that provides an architecture to store and manipulate a group of objects.
The main interfaces are Collection, List, Set, Map, and Queue.
A List allows duplicate elements and maintains the order of insertion, while a Set does not allow duplicates and does not guarantee order.
ArrayList class?ArrayList is a resizable array implementation of the List interface, allowing dynamic arrays that can grow as needed.
List<String> list = new ArrayList<>();
list.add("A");
list.add("B");
LinkedList class?LinkedList is a doubly-linked list implementation of the List interface, allowing for efficient insertions and deletions.
List<String> linkedList = new LinkedList<>();
linkedList.add("A");
linkedList.add("B");
HashSet class?HashSet is an implementation of the Set interface that uses a hash table for storage, allowing for fast access and no duplicate elements.
Set<String> set = new HashSet<>();
set.add("A");
set.add("B");
TreeSet class?TreeSet is a sorted set implementation that uses a red-black tree, maintaining elements in their natural order or according to a specified comparator.
Set<String> treeSet = new TreeSet<>();
treeSet.add("B");
treeSet.add("A");
HashMap class?HashMap is an implementation of the Map interface that uses a hash table for storage, allowing for fast retrieval of key-value pairs.
Map<String, Integer> map = new HashMap<>();
map.put("A", 1);
map.put("B", 2);
TreeMap class?TreeMap is a sorted map implementation that uses a red-black tree, maintaining keys in their natural order or according to a specified comparator.
Map<String, Integer> treeMap = new TreeMap<>();
treeMap.put("B", 2);
treeMap.put("A", 1);
LinkedHashMap class?LinkedHashMap is an implementation of the Map interface that maintains a linked list of the entries, preserving the order of insertion.
Map<String, Integer> linkedHashMap = new LinkedHashMap<>();
linkedHashMap.put("A", 1);
linkedHashMap.put("B", 2);
Queue interface?The Queue interface represents a collection designed for holding elements prior to processing, typically in a FIFO (first-in-first-out) order.
PriorityQueue class?PriorityQueue is an implementation of the Queue interface that orders elements based on their natural ordering or a specified comparator.
Queue<Integer> priorityQueue = new PriorityQueue<>();
priorityQueue.add(3);
priorityQueue.add(1);
Deque interface?The Deque interface represents a double-ended queue that allows elements to be added or removed from both ends.
ArrayDeque class?ArrayDeque is a resizable array implementation of the Deque interface, allowing for efficient insertion and removal of elements from both ends.
Deque<String> arrayDeque = new ArrayDeque<>();
arrayDeque.add("A");
arrayDeque.addFirst("B");
Collections class?The Collections class provides static methods for operating on collections, such as sorting and searching.
Collections.sort() method?You can sort a list using the Collections.sort() method, which sorts the list in natural order.
List<String> list = Arrays.asList("B", "A", "C");
Collections.sort(list);
Collections.reverse() method?You can reverse a list using the Collections.reverse() method.
Collections.reverse(list);
Comparator interface?The Comparator interface is used to define a custom ordering for objects, allowing for sorting based on specific criteria.
You can implement a custom comparator by creating a class that implements the Comparator interface.
class MyComparator implements Comparator<String> {
public int compare(String a, String b) {
return a.length() - b.length(); // Compare by length
}
}
You can use a comparator to sort a list by passing it to the Collections.sort() method.
Collections.sort(list, new MyComparator());
ListIterator interface?The ListIterator interface allows for iterating over a list in both forward and backward directions.
You can create a ListIterator by calling the list.listIterator() method on a list.
ListIterator<String> iterator = list.listIterator();
Map.Entry interface?The Map.Entry interface represents a key-value pair in a map.
You can iterate over a map using the entrySet() method to get a set of entries.
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
Collections.unmodifiableList() method?The Collections.unmodifiableList() method returns an unmodifiable view of the specified list.
List<String> unmodifiableList = Collections.unmodifiableList(list);
Collections.synchronizedList() method?The Collections.synchronizedList() method returns a synchronized (thread-safe) list backed by the specified list.
List<String> synchronizedList = Collections.synchronizedList(new ArrayList<>());
Collections.sort() method used for?The Collections.sort() method is used to sort a list in natural order or according to a specified comparator.
Collections.shuffle() method?The Collections.shuffle() method randomly permutes the elements in a list.
Collections.shuffle(list);
Collections.binarySearch() method?The Collections.binarySearch() method searches for a specified element in a sorted list using the binary search algorithm.
int index = Collections.binarySearch(list, "B");
Collections.fill() method?The Collections.fill() method replaces all elements in a list with the specified element.
Collections.fill(list, "X");
Collections.copy() method?The Collections.copy() method copies elements from one list to another.
Collections.copy(destinationList, sourceList);
Collections.indexOfSubList() method?The Collections.indexOfSubList() method returns the starting position of the first occurrence of a specified sublist in a list.
int index = Collections.indexOfSubList(list, subList);
Collections.rotate() method?The Collections.rotate() method rotates the elements in a list by a specified distance.
Collections.rotate(list, 2);
Collections.swap() method?The Collections.swap() method swaps the elements at the specified positions in a list.
Collections.swap(list, 0, 1);
List interface?The List interface is an ordered collection that allows duplicate elements and provides positional access to elements.
Set interface?The Set interface is a collection that does not allow duplicate elements and does not guarantee the order of elements.
Map interface?The Map interface is a collection that maps keys to values, allowing for unique keys and associated values.
Queue interface?The Queue interface represents a collection designed for holding elements prior to processing, typically in a FIFO (first-in-first-out) order.
Deque interface?The Deque interface represents a double-ended queue that allows elements to be added or removed from both ends.
Collections.unmodifiableSet() method?The Collections.unmodifiableSet() method returns an unmodifiable view of the specified set.
Set<String> unmodifiableSet = Collections.unmodifiableSet(set);
Collections.synchronizedSet() method?The Collections.synchronizedSet() method returns a synchronized (thread-safe) set backed by the specified set.
Set<String> synchronizedSet = Collections.synchronizedSet(new HashSet<>());
Collections.unmodifiableMap() method?The Collections.unmodifiableMap() method returns an unmodifiable view of the specified map.
Map<String, Integer> unmodifiableMap = Collections.unmodifiableMap(map);
Collections.synchronizedMap() method?The Collections.synchronizedMap() method returns a synchronized (thread-safe) map backed by the specified map.
Map<String, Integer> synchronizedMap = Collections.synchronizedMap(new HashMap<>());
Collections.nCopies() method?The Collections.nCopies() method returns an immutable list consisting of n copies of the specified object.
List<String> copies = Collections.nCopies(3, "A");
Collections.singletonList() method?The Collections.singletonList() method returns an immutable list containing only the specified object.
List<String> singleList = Collections.singletonList("A");
Collections.singletonMap() method?The Collections.singletonMap() method returns an immutable map containing a single key-value pair.
Map<String, Integer> singleMap = Collections.singletonMap("A", 1);
Collections.disjoint() method?The Collections.disjoint() method checks if two collections have no elements in common.
boolean disjoint = Collections.disjoint(list1, list2);
Collections.rotate() method?The Collections.rotate() method rotates the elements in a list by a specified distance.
Collections.rotate(list, 2);
Collections.swap() method?The Collections.swap() method swaps the elements at the specified positions in a list.
Collections.swap(list, 0, 1);
Collections.frequency() method?The Collections.frequency() method returns the number of occurrences of a specified element in a collection.
int frequency = Collections.frequency(list, "A");