The perfect place for easy learning...

Java Programming

×

Topics List


Collection algorithms in java





The java collection framework defines several algorithms as static methods that can be used with collections and map objects.

All the collection algorithms in the java are defined in a class called Collections which defined in the java.util package.

All these algorithms are highly efficient and make coding very easy. It is better to use them than trying to re-implement them.

The collection framework has the following methods as algorithms.

Method Description
void sort(List list) Sorts the elements of the list as determined by their natural ordering.
void sort(List list, Comparator comp) Sorts the elements of the list as determined by Comparator comp.
void reverse(List list) Reverses all the elements sequence in list.
void rotate(List list, int n) Rotates list by n places to the right. To rotate left, use a negative value for n.
void shuffle(List list) Shuffles the elements in list.
void shuffle(List list, Random r) Shuffles the elements in the list by using r as a source of random numbers.
void copy(List list1, List list2) Copies the elements of list2 to list1.
List nCopies(int num, Object obj) Returns num copies of obj contained in an immutable list. num can not be zero or negative.
void swap(List list, int idx1, int idx2) Exchanges the elements in the list at the indices specified by idx1 and idx2.
int binarySearch(List list, Object value) Returns the position of value in the list (must be in the sorted order), or -1 if value is not found.
int binarySearch(List list, Object value, Comparator c) Returns the position of value in the list ordered according to c, or -1 if value is not found.
int indexOfSubList(List list, List subList) Returns the index of the first match of subList in the list, or -1 if no match is found.
int lastIndexOfSubList(List list, List subList) Returns the index of the last match of subList in the list, or -1 if no match is found.
Object max(Collection c) Returns the largest element from the collection c as determined by natural ordering.
Object max(Collection c, Comparator comp) Returns the largest element from the collection c as determined by Comparator comp.
Object min(Collection c) Returns the smallest element from the collection c as determined by natural ordering.
Object min(Collection c, Comparator comp) Returns the smallest element from the collection c as determined by Comparator comp.
void fill(List list, Object obj) Assigns obj to each element of the list.
boolean replaceAll(List list, Object old, Object new) Replaces all occurrences of old with new in the list.
Enumeration enumeration(Collection c) Returns an enumeration over Collection c.
ArrayList list(Enumeration enum) Returns an ArrayList that contains the elements of enum.
Set singleton(Object obj) Returns obj as an immutable set.
List singletonList(Object obj) Returns obj as an immutable list.
Map singletonMap(Object k, Object v) Returns the key(k)/value(v) pair as an immutable map.
Collection synchronizedCollection(Collection c) Returns a thread-safe collection backed by c.
List synchronizedList(List list) Returns a thread-safe list backed by list.
Map synchronizedMap(Map m) Returns a thread-safe map backed by m.
SortedMap synchronizedSortedMap(SortedMap sm) Returns a thread-safe SortedMap backed by sm.
Set synchronizedSet(Set s) Returns a thread-safe set backed by s.
SortedSet synchronizedSortedSet(SortedSet ss) Returns a thread-safe set backed by ss.
Collection unmodifiableCollection(Collection c) Returns an unmodifiable collection backed by c.
List unmodifiableList(List list) Returns an unmodifiable list backed by list.
Set unmodifiableSet(Set s) Returns an unmodifiable thread-safe set backed by s.
SortedSet unmodifiableSortedSet(SortedSet ss) Returns an unmodifiable set backed by ss.
Map unmodifiableMap(Map m) Returns an unmodifiable map backed by m.
SortedMap unmodifiableSortedMap(SortedMap sm) Returns an unmodifiable SortedMap backed by sm.

Let's consider an example program to illustrate Collections algorithms.

Example
import java.util.*;

public class CollectionAlgorithmsExample {

	public static void main(String[] args) {

		ArrayList list = new ArrayList();
		PriorityQueue queue = new PriorityQueue();
		HashSet set = new HashSet();
		HashMap map = new HashMap();
		
		Random num = new Random();
		
		for(int i = 0; i < 5; i++) {
			list.add(num.nextInt(100));
			queue.add(num.nextInt(100));
			set.add(num.nextInt(100));
			map.put(i, num.nextInt(100));
		}
		
		System.out.println("List => " + list);
		System.out.println("Queue => " + queue);
		System.out.println("Set => " + set);
		System.out.println("Map => " + map);
		System.out.println("---------------------------------------");
		
		Collections.sort(list);
		System.out.println("List in ascending order => " + list);
		
		System.out.println("Largest element in set => " + Collections.max(set));
		
		System.out.println("Smallest element in queue => " + Collections.min(queue));
		
		Collections.reverse(list);
		System.out.println("List in reverse order => " + list);
		
		Collections.shuffle(list);
		System.out.println("List after shuffle => " + list);
	}
}

Collection algorithms in java