Java Collections swap(List<?>, int, int) Method



Description

The Java Collections swap(List<?>, int, int) method is used to swap the elements at the specified positions in the specified list.

Declaration

Following is the declaration for java.util.Collections.swap() method.

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

Parameters

  • list − The list in which to the swap elements.

  • i − The index of one element to be swapped.

  • j − The index of the other element to be swapped.

Return Value

NA

Exception

IndexOutOfBoundsException − This is thrown if either i or j is out of range (i < 0 || i >= list.size() || j < 0 || j >= list.size()).

Swapping elements of List of Integer Example

The following example shows the usage of Java Collection swap(List,int, int) method. We've created a List object with some integers, printed the original list. Using swap(List, int, int) method, we've swapped elements of 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<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5));

      System.out.println("Initial collection value: " + list);
      // swap values of this collection
      Collections.swap(list, 0, 4);
      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: [5, 2, 3, 4, 1]

Swapping elements of List of String Example

The following example shows the usage of Java Collection swap(List,int, int) method. We've created a List object with some strings, printed the original list. Using swap(List, int, int) method, we've swapped elements of 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<String> list = new ArrayList<>(Arrays.asList("A","B","C","D","E"));

      System.out.println("Initial collection value: " + list);
      // swap values of this collection
      Collections.swap(list, 0, 4);
      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: [A, B, C, D, E]
Final collection value: [E, B, C, D, A]

Swapping elements of List of Object Example

The following example shows the usage of Java Collection swap(List,int, int) method. We've created a List object with some Student objects, printed the original list. Using swap(List, int, int) method, we've swapped elements of 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<Student> list = new ArrayList<>(Arrays.asList(new Student(1, "Julie"),
         new Student(2, "Robert"), new Student(3, "Adam")));

      System.out.println("Initial collection value: " + list);
      // swap values of this collection
      Collections.swap(list, 0, 2);
      System.out.println("Final collection value: "+list);
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

Output

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

Initial collection value: [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
Final collection value: [[ 3, Adam ], [ 2, Robert ], [ 1, Julie ]]
java_util_collections.htm
Advertisements