Menu
Topics Index
...
`


Collections Framework > Collection Algorithms >
Siva Nookala - 22 Mar 2016
There are lot of predefined algorithms for working with the Collections. These algorithms are highly efficient and it is better to use them than trying to re-implement them.

All the below mentioned methods are static and are available on the java.util.Collections class.
MethodDescription
<T> boolean addAll(Collection <? super T> c, T ... elements)Adds all the passed elements to the given collection. This is useful when we want add multiple values to the same collection. Than calling add method multiple times, we can use addAll.
<T>Queue<T>asLifoQueue(Deque<T> c)Returns a last-in, first-out view of c.
<T> int binarySearch(List<? extends T> list, T value, Comparator<? super T> c)Performs a binary search for the given value in the given list using the passed Comparator c.
<T> int binarySearch(List<? extends Comparable<? super T>> list, T value)Performs a binary search for the given value from the list of Comparable objects. Note that if the objects are not Comparable, then we have to use the previous binarySearch method, which takes the Comparator.
<E>Collection<E>checkedCollection(Collection<E> c, Class<E> t)Returns a run-time type-safe view of a collection. An attempt to insert an incompatible element will cause a ClassCastException
<T> void copy(List<? super T> destination, List<? extends T> source)Copies from source list into the destination list. The destination list will be emptied before copying and source list will not be changed.
boolean disjoint(Collection<?> a, Collection<?> b)Returns true, if both the collections do not have even one common element. Even if one common element is present, then it returns false.
int frequency(Collection<?> c, Object obj)Counts the number of times an object is present in a Collection.
int indexOfSubList(List<?> list, List<?> subList)Searches for a sub list inside the main list. If we want to find where [C, A, T] in [T, R, Y, C, A, T, C, H] then we can use this.
int lastIndexOfSubList(List<?> list, List<?> subList)Similar to indexOfSubList, just that it searches from the end or last.
<T> T max(Collection<? extends T> c, Comparator<? super T> comp)Finds the maximum element in the collection using the passed comparator.
<T extends Object & Comparable<? super T>> T max(Collection<? extends T> c)Finds the maximum element in the collection of Comparable objects.
<T> T min(Collection<? extends T> c, Comparator<? super T> comp)Finds the minimum element in the collection using the passed comparator.
<T extends Object & Comparable<? super T>> T min(Collection<? extends T> c)Finds the minimum element in the collection of Comparable objects.
<T> boolean replaceAll(List<T> list, T old, T new)Replaces all occurrences of old with new in list. Returns true if at least one replacement occurred. Returns false, otherwise.
void reverse(List<T> list)Reverses the sequence in list.
void rotate(List<T> list, int n)Rotates list by n places to the right. To rotate left, use a negative value for n.
void shuffle(List<T> list, Random r)Shuffles (i.e., randomizes) the elements in list by using r as a source of random numbers.
void shuffle(List<T> list)Shuffles (i.e., randomizes) the elements in list.
void sort(List<T>list, Comparator<? super T> comp)Sorts the elements of list as determined by comp
void sort(List<T>list)Sorts the elements of list as determined by their natural ordering.
void swap(List<?>list, int idx1, int idx2)Exchanges the elements in the list at the indices specified by idx1 and idx2
<T>Lst<T> emptyList()Returns an immutable, empty List object of the inferred type.
<T>Set<T> emptySet()Returns an immutable, empty Set object of the inferred type.
void fill(List<? super T> list, T obj)Assigns obj to each element of list.
The other important collection algorithms are discussed in Java Read-only Collections And Algorithms , Java Thread Safe Collections & Algorithms, Java Singleton and Java nCopies Collections - Collections.nCopies() Method,

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.