1. List of methods

Remember that Java's creators wrote a whole helper class called Arrays for our convenience when working with arrays?

They did the same thing for collections. Java has a java.util.Collections class that has a lot of methods that are useful for working with collections. Here are just the most interesting ones:

Methods Description
addAll(colls, e1, e2, e3, ...)
Adds the elements e1, e2, e3, ... to the colls collection
fill(list, obj)
Replaces all the elements in the passed list with obj
nCopies(n, obj)
Returns a list of n copies of the obj object
replaceAll(list, oldVal, newVal)
Replaces all instances of oldVal with newVal in the list list
copy(dest, src)
Copies all elements from the src list to the dest list
reverse(list)
Reverses the list.
sort(list)
Sorts the list in ascending order
rotate(list, n)
Cyclically shifts the elements of the list list by n elements
shuffle(list)
Randomly shuffles the elements in the list
min(colls)
Finds the minimum element in the colls collection
max(colls)
Finds the maximum element in the colls collection
frequency(colls, obj)
Determines how many times the obj element occurs in the colls collection
binarySearch(list, key)
Searches for key in a sorted list and returns the corresponding index.
disjoint(colls1, colls2)
Returns true if the collections have no elements in common
Important:

Many of these methods don't use the ArrayList, HashSet and HashMap classes per se, but with the corresponding interfaces: Collection<T>, List<T>, Map<K, V>.

This is not a problem: if a method accepts a List<T>, you can always pass it an ArrayList<Integer>, but the assignment operator does not work in the opposite direction.


2. Creating and modifying collections

Collections.addAll(Collection<T> colls, T e1, T e2, T e3, ...) method

The addAll() method adds the elements e1, e2, e3, ... to the colls collection Any number of elements can be passed.

Code Console output
ArrayList<Integer> list = new ArrayList<Integer>();
Collections.addAll(list, 1, 2, 3, 4, 5);

for (int i: list)
   System.out.println(i);
1
2
3
4
5

Collections.fill(List<T> list, T obj) method

The fill() method replaces all the elements of the list collection with the obj element.

Code Console output
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);

Collections.fill(list, 10);

for (int i: list)
   System.out.println(i);
10
10
10

Collections.nCopies(int n, T obj) method

The nCopies() method returns a list of n copies of the obj element. Note that the returned list is immutable, which means you cannot change it! You can only use it to read values:

Code Description
List<String> immutableList = Collections.nCopies(5, "Hello");

ArrayList<String> list = new ArrayList<String>(immutableList);

for(String s: list)
   System.out.println(s);
Create an immutable list of 5 Hello strings
Create a mutable list and fill it with the values from the immutableList list.

Console output:
Hello
Hello
Hello
Hello
Hello

Collections.replaceAll (List<T> list, T oldValue, T newValue) method

The replaceAll() method replaces all the elements in the list collection equal to oldValue with newValue.

Code Console output
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);

Collections.replaceAll(list, 2, 20);

for (int i: list)
   System.out.println(i);
1
20
3

Collections.copy (List<T> dest, List<T> src) method

The copy() method copies all the elements of the src collection into the dest collection.

If the dest collection starts out longer than the src collection, then the remaining elements of the dest collection will remain intact.

Important:
The dest collection must be at least as long as the src collection (otherwise, an IndexOutOfBoundsException will be thrown).
Code Console output
ArrayList<Integer> srcList = new ArrayList<Integer>();
Collections.addAll(srcList, 99, 98, 97);

ArrayList<Integer> destList = new ArrayList<Integer>();
Collections.addAll(destList, 1, 2, 3, 4, 5, 6, 7);

Collections.copy(destList, srcList);

for (int i: destList)
   System.out.println(i);
99
98
97
4
5
6
7


3. Order of the elements

Collections.reverse(List<T> list) method

The reverse() method reverses the order of the elements of the passed list.

Code Console output
ArrayList<Integer> list = new ArrayList<Integer>();
Collections.addAll(list, 1, 2, 3, 4, 5);

Collections.reverse(list);

for (int i: list)
   System.out.println(i);
5
4
3
2
1

Collections.sort(List<T> list) method

The sort() method sorts the passed list in ascending order.

Code Console output
ArrayList<Integer> list = new ArrayList<Integer>();
Collections.addAll(list, 11, 2, 23, 4, 15);

Collections.sort(list);

for (int i: list)
   System.out.println(i);
2
4
11
15
23

Collections.rotate(List<T> list, int distance) method

The rotate() method cyclically shifts the elements of the passed list by distance positions forward.

Code Console output
ArrayList<Integer> list = new ArrayList<Integer>();
Collections.addAll(list, 1, 2, 3, 4, 5, 6, 7, 8, 9);

Collections.rotate(list, 3); // Shift by 3 positions

for (int i: list)
   System.out.println(i);
7
8
9
1
2
3
4
5
6

Collections.shuffle(List<T> list) method

The shuffle() method randomly shuffles all the elements of the passed list. The result is different every time.

Code Console output
ArrayList<Integer> list = new ArrayList<Integer>();
Collections.addAll(list, 1, 2, 3, 4, 5);

Collections.shuffle(list); // Shuffle the elements

for (int i: list)
   System.out.println(i);
5
2
4
3
1


4. Finding elements in collections

Collections.min(Collection<T> colls) method

The min() method returns the minimum element in the collection.

Code Console output
ArrayList<Integer> list = new ArrayList<Integer>();
Collections.addAll(list, 11, 2, 23, 4, 15);

int min = Collections.min(list);

System.out.println(min);
2

Collections.max(Collection<T> colls) method

The max() method returns the maximum element in the collection.

Code Console output
ArrayList<Integer> list = new ArrayList<Integer>();
Collections.addAll(list, 11, 2, 23, 4, 15);

int max = Collections.max(list);

System.out.println(max);
23

Collections.frequency(Collection<T> colls, T obj) method

The frequency() method counts the number of times the obj element occurs in the colls collection

Code Console output
ArrayList<Integer> list = new ArrayList<Integer>();
Collections.addAll(list, 11, 2, 23, 4, 15, 4, 2, 4);

int count = Collections.frequency(list, 4);

System.out.println(count);
3

Collections.binarySearch(Collection<T> colls, T obj) method

The binarySearch() method searches for the obj element in the colls collection. Returns the index of the found element. Returns a negative number if the element is not found.

Important:
Before calling the binarySearch() method, the collection must be sorted (use Collections.sort()).
Code Console output
ArrayList<Integer> list = new ArrayList<Integer>();
Collections.addAll(list, 11, 2, 23, 5, 15, 4, 2, 4);

Collections.sort(list);  // 2, 2, 4, 4, 5, 11, 15, 23

int index = Collections.binarySearch(list, 5);    // 4
System.out.println(index);

int index2 = Collections.binarySearch(list, 15);  // 6
System.out.println(index2);

int index3 = Collections.binarySearch(list, 16); // The element doesn't exist
System.out.println(index3);
4
6
-8

Collections.disjoint(Collection<T> coll1, Collection<T> coll2) method

The disjoint() method returns true if the passed collections do not have any elements in common.

Code Console output
ArrayList<Integer> list = new ArrayList<Integer>();
Collections.addAll(list, 1, 2, 3, 4, 5, 6, 7);

ArrayList<Integer> list2 = new ArrayList<Integer>();
Collections.addAll(list2, 99, 98, 97);

boolean isDifferent = Collections.disjoint(list, list2);
System.out.println(isDifferent);
true