This Java tutorial helps you understand the usage of the Collections utility class for ordering and manipulating elements in a List collection.

The java.util.Collections class provides reusable functionalities that operation on collections such as changing the order of list elements and changing the content of a list. These are grouped into “generic algorithms” category. In this article, we help you understand how to use these functionalities with code examples.

 

1. Changing the order of list elements

The functions in this group allow us to reverse, rotate, permute, sort and swap elements in a list.

Reverse elements in a list:

Method:

public static void reverse(List<?> list)

Code example:

List<String> listFruits = Arrays.asList("Orange", "Apple",
			"Banana", "Lemon", "Grape", "Cherry", "Coconut");

System.out.println("Before reverse:\n " + listFruits);

Collections.reverse(listFruits);

System.out.println("After reverse:\n " + listFruits);
 

Output:

Before reverse:
 [Orange, Apple, Banana, Lemon, Grape, Cherry, Coconut]
After reverse:
 [Coconut, Cherry, Grape, Lemon, Banana, Apple, Orange]
 

Rotate elements in a list:



The following method rotates the elements in the specified list by the specified distance:

public static void rotate(List<?> list, int distance)

Code example:

List<Integer> listNumbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);

System.out.println("Before rotate:\n " + listNumbers);

Collections.rotate(listNumbers, 3);

System.out.println("After rotate:\n " + listNumbers);
Output:

Before rotate:
 [1, 2, 4, 5, 6, 7, 8, 9]
After rotate:
 [7, 8, 9, 1, 2, 3, 4, 5, 6]
You can use this method in conjunction with the subList() method to rotate only a range of elements in the list, while other elements remain unchanged. For example:

List<Integer> listNumbers = Arrays.asList(1, 2, 4, 5, 6, 7, 8, 9);

System.out.println("Before rotate:\n " + listNumbers);

Collections.rotate(listNumbers.subList(3, 6), 2);

System.out.println("After rotate:\n " + listNumbers);
Output:

Before rotate:
 [1, 2, 3, 4, 5, 6, 7, 8, 9]
After rotate:
 [1, 2, 3, 5, 6, 4, 7, 8, 9]
 

Permute elements in a list:

The following methods permute the specified list:

public static void shuffle(List<?> list)
public static void shuffle(List<?> list, Random rnd)

The former method permutes a list using a default source of randomness, whereas the latter uses the specified source of randomness.

Code example:

List<Integer> listNumbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);

System.out.println("Before shuffle:\n " + listNumbers);

Collections.shuffle(listNumbers);

System.out.println("After shuffle:\n " + listNumbers);
Output:

Before shuffle:
 [1, 2, 3, 4, 5, 6, 7, 8, 9]
After shuffle:
 [7, 2, 1, 6, 8, 4, 9, 5, 3]
 

Sort elements in a list:

The following method sorts the specified list into ascending order, according to the natural ordering of its elements (all elements must implement the Comparableinterface):

public static void sort(List<T> list)

Note that the specified list must be modifiable, and the implementation in JDK uses mergesort algorithm.

Code example:

List<String> listNames = Arrays.asList("Tim",
		"Anne", "Dave", "Jean", "John", "Peter", "Carol");

System.out.println("Before sort: " + listNames);

Collections.sort(listNames);

System.out.println("After sort: " + listNames);
Output:

Before sort: [Tim, Anne, Dave, Jean, John, Peter, Carol]
After sort: [Anne, Carol, Dave, Jean, John, Peter, Tim]
For a comprehensive code examples about sorting, see the article: Sorting List Collections Examples

 

Swap elements in a list:

The following method swaps the elements at the specified positions in the specified list:

public static void swap(List<?> list, int i, int j)

Code example:

List<String> listCountries = Arrays.asList("USA",
			"Japan", "UK", "France", "Canada", "Singapore");

System.out.println("Before swap: " + listCountries);

Collections.swap(listCountries, 1, 5);

System.out.println("After swap: " + listCountries);
Output:

Before swap: [USA, Japan, UK, France, Canada, Singapore]
After swap: [USA, Singapore, UK, France, Canada, Japan]
 

2. Changing the content of a list

The methods in this group allow us to replace elements in a list.

Copy elements from one list to another:

The following method copies all of the elements from the source list (src) to the destination list (dest):

public static <T> void copy(List<? super T> dest, List<? extends T> src)

The copied elements replace the elements in the destination list at the same index. Note that the destination list must be at least as long as the source list. If it is longer, the remaining elements in the destination list are unaffected.

Code example:

List<Integer> source = Arrays.asList(2, 4, 6, 8, 10);
List<Integer> dest = Arrays.asList(1, 3, 5, 7, 9, 11, 13, 15);

System.out.println("dest before copy: " + dest);

Collections.copy(dest, source);

System.out.println("dest after copy: " + dest);
Output:

dest before copy: [1, 3, 5, 7, 9, 11, 13, 15]
dest after copy: [2, 4, 6, 8, 10, 11, 13, 15]
 

Replace all elements in a list with a specified element:

The following method replaces all of the elements of the specified list with the specified element:

public static <T> void fill(List<? super T> list, T obj)

Code example:

List<Integer> listNumbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);

System.out.println("Before fill:\n " + listNumbers);

Collections.fill(listNumbers, 0);

System.out.println("After fill:\n " + listNumbers);
Output:

Before fill:
 [1, 2, 3, 4, 5, 6, 7, 8, 9]
After fill:
 [0, 0, 0, 0, 0, 0, 0, 0, 0]
 

Replace all occurrences of an element with another:

The following method replaces all occurrences of one specified value in the specified list with another:

public static <T> boolean replaceAll(List<T> list, T oldVal, T newVal)

Code example:

List<String> listLetters
	= Arrays.asList("h", "e", "l", "l", "o", "w", "o", "r", "l", "d");

System.out.println("Before replace:\n " + listLetters);

Collections.replaceAll(listLetters, "l", "L");

System.out.println("After replace:\n " + listLetters);
Output:

Before replace:
 [h, e, l, l, o, w, o, r, l, d]
After replace:
 [h, e, L, L, o, w, o, r, L, d]

 

References:

 

Related Collection Utility Classes Tutorials:

 

Other Java Collections Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Attachments:
Download this file (CollectionsChangeOrderAndContentExamples.java)CollectionsChangeOrderAndContentExamples.java[Java Collections Code Example]2 kB

Add comment