Collections swap() method in Java with Examples

The swap() method of java.util.Collections class is used to swap the elements at the specified positions in the specified list. If the specified positions are equal, invoking this method leaves the list unchanged.

Syntax:

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

Parameters: This method takes the following argument as a Parameter

  • list – The list in which to swap elements.
  • i – the index of one element to be swapped.
  • j – the index of the other element to be swapped.

Exception This method throws IndexOutOfBoundsException, if either i or j is out of range (i = list.size() || j = list.size()).

Below are the examples to illustrate the swap() method



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// swap() method for String value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // creating object of List<String>
            List<String> vector = new ArrayList<String>();
  
            // populate the vector
            vector.add("A");
            vector.add("B");
            vector.add("C");
            vector.add("D");
            vector.add("E");
  
            // pritning the vector before swap
            System.out.println("Before swap: " + vector);
  
            // swap the elements
            System.out.println("\nSwapping 0th and 4th element.");
            Collections.swap(vector, 0, 4);
  
            // priting the vector after swap
            System.out.println("\nAfter swap: " + vector);
        }
  
        catch (IndexOutOfBoundsException e) {
            System.out.println("\nException thrown : " + e);
        }
    }
}

chevron_right


Output:

Before swap: [A, B, C, D, E]

Swapping 0th and 4th element.

After swap: [E, B, C, D, A]

Example 2: For IndexOutOfBoundsException

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// swap() method for IndexOutOfBoundsException
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
  
            // creating object of List<String>
            List<String> vector = new ArrayList<String>();
  
            // populate the vector
            vector.add("A");
            vector.add("B");
            vector.add("C");
            vector.add("D");
            vector.add("E");
  
            // pritning the vector before swap
            System.out.println("Before swap: " + vector);
  
            // swap the elements
            System.out.println("\nTrying to swap elements"
                               + " more than upper bound index ");
            Collections.swap(vector, 0, 5);
  
            // priting the vector after swap
            System.out.println("After swap: " + vector);
        }
  
        catch (IndexOutOfBoundsException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

Before swap: [A, B, C, D, E]

Trying to swap elements more than upper bound index 
Exception thrown : java.lang.IndexOutOfBoundsException: Index: 5, Size: 5


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.