The Java Collections Framework provides a comprehensive set of interfaces, implementations, and algorithms to manipulate and work with collections of objects. The framework simplifies the process of handling groups of objects, offering efficiency, type-safety, and flexibility. Part of this framework includes various algorithms designed to perform common tasks on collections, enhancing productivity and code quality.
In Java, the Collection Framework algorithms refer to the utility methods provided by the java.util.Collections
class to perform common operations such as sorting, searching, reversing, shuffling, and more on collections like List
, Set
, etc.
These are static methods, and work primarily with List
objects, although some work with other collections.
You need to import this class to access its static algorithm methods.
Common Collection Framework Algorithms
(All are static methods)
public static <T extends Comparable<? super T>> void sort(List<T> list) |
Sorts the list in natural ascending order. |
public static <T> void sort(List<T> list, Comparator<? super T> c) |
Sorts the list using a custom comparator. |
public static void reverse(List<?> list) |
Reverses the order of elements in the list. |
public static void shuffle(List<?> list) |
Randomly shuffles elements in the list. |
public static void swap(List<?> list, int i, int j) |
Swaps elements at specified positions. |
public static <T> void fill(List<? super T> list, T obj) |
Replaces all elements with the given object. |
public static <T> void copy(List<? super T> dest, List<? extends T> src) |
Copies elements from source to destination list. |
public static <T extends Comparable<? super T>> int binarySearch(List<? extends T> list, T key) |
Searches for the key using binary search. |
public static <T> int binarySearch(List<? extends T> list, T key, Comparator<? super T> c) |
Binary search using custom comparator. |
public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll) |
Returns the maximum element by natural order. |
public static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp) |
Returns the maximum element using comparator. |
public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll) |
Returns the minimum element by natural order. |
public static <T> T min(Collection<? extends T> coll, Comparator<? super T> comp) |
Returns the minimum element using comparator. |
public static int frequency(Collection<?> c, Object o) |
Counts how many times the object appears in the collection. |
public static boolean disjoint(Collection<?> c1, Collection<?> c2) |
Returns true if collections have no common elements. |
public static <T> boolean replaceAll(List<T> list, T oldVal, T newVal) |
Replaces all occurrences of old value with new value. |
public static <T> List<T> nCopies(int n, T o) |
Returns an immutable list of n copies of the object. |
public static <T> Comparator<T> reverseOrder() |
Returns a comparator that reverses natural ordering. |
public static <T> Comparator<T> reverseOrder(Comparator<T> cmp) |
Returns a comparator that reverses the given comparator. |
public static <E> Collection<E> checkedCollection(Collection<E> c, Class<E> type) |
Returns a type-safe view of the collection. |
public static <E> List<E> checkedList(List<E> list, Class<E> type) |
Returns a type-safe view of the list. |
public static <E> Set<E> checkedSet(Set<E> s, Class<E> type) |
Returns a type-safe view of the set. |
public static <K,V> Map<K,V> checkedMap(Map<K,V> m, Class<K> keyType, Class<V> valueType) |
Returns a type-safe view of the map. |
public static final <T> List<T> emptyList() |
Returns an immutable empty list. |
public static final <T> Set<T> emptySet() |
Returns an immutable empty set. |
public static final <K,V> Map<K,V> emptyMap() |
Returns an immutable empty map. |
public static <T> List<T> singletonList(T o) |
Returns an immutable list with one element. |
public static <T> Set<T> singleton(T o) |
Returns an immutable set with one element. |
public static <K,V> Map<K,V> singletonMap(K key, V value) |
Returns an immutable map with one key-value pair. |
public static <T> List<T> unmodifiableList(List<? extends T> list) |
Returns a read-only view of the list. |
public static <T> Set<T> unmodifiableSet(Set<? extends T> s) |
Returns a read-only view of the set. |
public static <K,V> Map<K, V> unmodifiableMap(Map<? extends K, ? extends V> m) |
Returns a read-only view of the map. |
Sample Usage:
List<Integer> numbers = Arrays.asList(5, 3, 8, 1, 9);
Collections.sort(numbers); // void
int max = Collections.max(numbers); // int
boolean changed = Collections.replaceAll(numbers, 3, 33); // boolean
int index = Collections.binarySearch(numbers, 8); // int
List<Integer> repeated = Collections.nCopies(5, 7); // List<Integer>
import java.util.*; public class CollectionsDemo { public static void main(String[] args) { List<String> names = new ArrayList<>(Arrays.asList("Mahesh", "LotusJavaPrince", "Sita", "Rama")); // Sorting Collections.sort(names); System.out.println("Sorted: " + names); // Reversing Collections.reverse(names); System.out.println("Reversed: " + names); // Shuffling Collections.shuffle(names); System.out.println("Shuffled: " + names); // Maximum String max = Collections.max(names); System.out.println("Max: " + max); // Frequency of "Mahesh" int freq = Collections.frequency(names, "Mahesh"); System.out.println("Frequency of Mahesh: " + freq); } } /* D:\>javac CollectionsDemo.java D:\>java CollectionsDemo Sorted: [LotusJavaPrince, Mahesh, Rama, Sita] Reversed: [Sita, Rama, Mahesh, LotusJavaPrince] Shuffled: [Mahesh, LotusJavaPrince, Sita, Rama] Max: Sita Frequency of Mahesh: 1 */