Java Collections Class



Introduction

The Java Collections class consists exclusively of static methods that operate on or return collections.Following are the important points about Collections −

  • It contains polymorphic algorithms that operate on collections, "wrappers", which return a new collection backed by a specified collection.

  • The methods of this class all throw a NullPointerException if the collections or class objects provided to them are null.

Class declaration

Following is the declaration for java.util.Collections class −

public class Collections
   extends Object

Field

Following are the fields for java.util.Collections class −

  • static List EMPTY_LIST − This is the empty list (immutable).

  • static Map EMPTY_MAP − This is the empty map (immutable).

  • static Set EMPTY_SET − This is the empty set (immutable).

Class methods

Sr.No. Method & Description
1 static boolean addAll(Collection c, T... elements)

This method adds all of the specified elements to the specified collection.

2 static Queue asLifoQueue(Deque deque)

This method returns a view of a Deque as a Last-in-first-out (Lifo) Queue.

3 static int binarySearch(List> list, T key)

This method searches the specified list for the specified object using the binary search algorithm.

4 static Collection checkedCollection(Collection c, Class type)

This method returns a dynamically typesafe view of the specified collection.

5 static List checkedList(List list, Class type)

This method returns a dynamically typesafe view of the specified list.

6 static Map checkedMap(Map m, Class keyType, Class valueType)

This method returns a dynamically typesafe view of the specified map.

7 static Navigable checkedNavigableMap(Navigable m, Class keyType, Class valueType)

This method returns a dynamically typesafe view of the specified navigable map.

8 static NavigableSet checkedNavigableSet(NavigableSet s, Class type)

This method returns a dynamically typesafe view of the specified navigable set.

9 static Queue checkedQueue(Queue s, Class type)

This method returns a dynamically typesafe view of the specified queue.

10 static Set checkedSet(Set s, Class type)

This method returns a dynamically typesafe view of the specified set.

11 static SortedMap checkedSortedMap(SortedMap m, Class keyType, Class valueType)

This method returns a dynamically typesafe view of the specified sorted map.

12 static SortedSetcheckedSortedSet(SortedSet s, Class type)

This method returns a dynamically typesafe view of the specified sorted set.

13 static void copy(List dest, List src)

This method copies all of the elements from one list into another.

14 static boolean disjoint(Collection c1, Collection c2)

This method returns true if the two specified collections have no elements in common.

15 static Enumeration emptyEnumeration()

This method returns the empty enumeration.

16 static Iterator emptyIterator()

This method returns the empty iterator.

17 static List emptyList()

This method returns the empty list (immutable).

18 static ListIterator emptyListIterator()

This method returns the empty list iterator.

19 static Map emptyMap()

This method returns the empty map (immutable).

20 static NavigableMap emptyNavigableMap()

This method returns the empty navigable map (immutable).

21 static NavigableSet emptyNavigableSet()

This method returns the empty navigable set (immutable).

22 static Set emptySet()

This method returns the empty set (immutable).

23 static SortedMap emptySortedMap()

This method returns the empty sorted map (immutable).

24 static SortedSet emptySortedSet()

This method returns the empty sorted set (immutable).

25 static Enumeration enumeration(Collection c)

This method returns an enumeration over the specified collection.

26 static void fill(List list, T obj)

This method replaces all of the elements of the specified list with the specified element.

27 static int frequency(Collection c, Object o)

This method returns the number of elements in the specified collection equal to the specified object.

28 static int indexOfSubList(List source, List target)

This method returns the starting position of the first occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence.

29 static int lastIndexOfSubList(List source, List target)

This method returns the starting position of the last occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence.

30 static ArrayList list(Enumeration e)

This method returns an array list containing the elements returned by the specified enumeration in the order they are returned by the enumeration.

31 static >T max(Collection coll)

This method returns the maximum element of the given collection, according to the natural ordering of its elements.

32 static >T min(Collection coll)

This method Returns the minimum element of the given collection, according to the natural ordering of its elements.

33 static List nCopies(int n, T o)

This method returns an immutable list consisting of n copies of the specified object.

34 static Set newSetFromMap(Map map)

This method returns a set backed by the specified map.

35 static boolean replaceAll(List list, T oldVal, T newVal)

This method replaces all occurrences of one specified value in a list with another.

36 static void reverse(List list)

This method reverses the order of the elements in the specified list.

37 static Comparator reverseOrder()

This method returns a comparator that imposes the reverse of the natural ordering on a collection of objects that implement the Comparable interface.

38 static void rotate(List list, int distance)

This method rotates the elements in the specified list by the specified distance.

39 static void shuffle(List list)

This method randomly permutes the specified list using a default source of randomness.

40 static Set singleton(T o)

This method returns an immutable set containing only the specified object.

41 static List singletonList(T o)

This method returns an immutable list containing only the specified object.

42 static Map singletonMap(K key, V value)

This method returns an immutable map, mapping only the specified key to the specified value.

43 static > void sort(List list)

This method sorts the specified list into ascending order, according to the natural ordering of its elements.

44 static void swap(List list, int i, int j)

This method swaps the elements at the specified positions in the specified list.

45 static Collection synchronizedCollection(Collection c)

This method returns a synchronized (thread-safe) collection backed by the specified collection.

46 static List synchronizedList(List list)

This method returns a synchronized (thread-safe) list backed by the specified list.

47 static Map synchronizedMap(Map m)

This method returns a synchronized (thread-safe) map backed by the specified map.

48 static NavigableMap synchronizedNavigableMap()

This method returns the synchronized navigable map (immutable).

49 static NavigableSet synchronizedNavigableSet()

This method returns the synchronized navigable set (immutable).

50 static Set synchronizedSet(Set s)

This method returns a synchronized (thread-safe) set backed by the specified set.

51 static SortedMap synchronizedSortedMap(SortedMap m)

This method returns a synchronized (thread-safe) sorted map backed by the specified sorted map.

52 static SortedSet synchronizedSortedSet(SortedSet s)

This method returns a synchronized (thread-safe) sorted set backed by the specified sorted set.

53 static Collection unmodifiableCollection(Collection c)

This method returns an unmodifiable view of the specified collection.

54 static List unmodifiableList(List list)

This method returns an unmodifiable view of the specified list.

55 static Map unmodifiableMap(Map m)

This method returns an unmodifiable view of the specified map.

56 static NavigableMap unmodifiableNavigableMap()

This method returns the unmodifiable navigable map (immutable).

57 static NavigableSet unmodifiableNavigableSet()

This method returns the unmodifiable navigable set (immutable).

58 static Set unmodifiableSet(Set s)

This method returns an unmodifiable view of the specified set.

59 static SortedMap unmodifiableSortedMap(SortedMap m)

This method returns an unmodifiable view of the specified sorted map.

60 static SortedSet unmodifiableSortedSet(SortedSet s)

This method returns an unmodifiable view of the specified sorted set.

Methods inherited

This class inherits methods from the following classes −

  • java.util.Object

Adding Multiple Elements to the Collection of Integers Example

The following example shows the usage of Java Collection addAll(Collection,T... ) method to add a collection of Integers. We've created a List object with some integers, printed the original list. Using addAll(collection, T...) method, we've added few more elements to the list and then printed the updated list.

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CollectionsDemo {

   public static void main(String[] args) {
      List list = new ArrayList<>(Arrays.asList(1,2,3,4,5));

      System.out.println("Initial collection value: " + list);
      // add values to this collection
      Collections.addAll(list, 6, 7, 8);
      System.out.println("Final collection value: "+list);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Initial collection value: [1, 2, 3, 4, 5]
Final collection value: [1, 2, 3, 4, 5, 6, 7, 8]
Advertisements