Open In App

Swapping items of a list in Java : Collections.swap() with Example

Last Updated : 11 Dec, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

java.util.Collections.swap() method is a java.util.Collections class method. It swaps elements at the specified positions in given list.

// Swaps elements at positions "i" and "j" in myList.
public static void swap(List mylist, int i, int j)

It throws IndexOutOfBoundsException if either i
or j is out of range.




// Java program to demonstrate working of Collections.swap
import java.util.*;
  
public class GFG
{
    public static void main(String[] args)
    {
        // Let us create a list with 4 items
        ArrayList<String>  mylist =
                        new ArrayList<String>();
        mylist.add("code");
        mylist.add("practice");
        mylist.add("quiz");
        mylist.add("geeksforgeeks");
  
        System.out.println("Original List : \n" + mylist);
  
        // Swap items at indexes 1 and 2
        Collections.swap(mylist, 1, 2);
  
        System.out.println("\nAfter swap(mylist, 1, 2) : \n"
                           + mylist);
  
        // Swap items at indexes 1 and 3
        Collections.swap(mylist, 3, 1);
  
        System.out.println("\nAfter swap(mylist, 3, 1) : \n"
                           + mylist);
    }
}


Output:

Original List : Original List : 
[code, practice, quiz, geeksforgeeks]

After swap(mylist, 1, 2) : 
[code, quiz, practice, geeksforgeeks]

After swap(mylist, 3, 1) : 
[code, geeksforgeeks, practice, quiz]

.


Similar Reads

Difference between Traditional Collections and Concurrent Collections in java
We all know about Traditional Collections ( i.e. List, Set, Queue and its implemented Classes) and Concurrent Collection (i.e. ConcurrentMap interface, ConcurrentHashMap class, CopyOnWriteArrayList class etc). In these two Collections, there are few differences like: Most of the Classes which are present in Traditional Collections (i.e ArrayList, L
3 min read
Java Program For Swapping Nodes In A Linked List Without Swapping Data
Given a linked list and two keys in it, swap nodes for two given keys. Nodes should be swapped by changing links. Swapping data of nodes may be expensive in many situations when data contains many fields. It may be assumed that all keys in the linked list are distinct. Examples: Input : 10-&gt;15-&gt;12-&gt;13-&gt;20-&gt;14, x = 12, y = 20 Output:
6 min read
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
2 min read
Collections.singleton() method in Java with example
java.util.Collections.singleton() method is a java.util.Collections class method. It creates a immutable set over a single specified element. An application of this method is to remove an element from Collections like List and Set. Syntax: public static Set singleton(T obj) and public static List singletonList(T obj) Parameters: obj : the sole obje
3 min read
Collection vs Collections in Java with Example
Collection: Collection is a interface present in java.util package. It is used to represent a group of individual objects as a single unit. It is similar to the container in the C++ language. The collection is considered as the root interface of the collection framework. It provides several classes and interfaces to represent a group of individual
3 min read
Collections list() method in Java with Examples
The list() method of java.util.Collections class is used to return an array list containing the elements returned by the specified enumeration in the order they are returned by the enumeration. This method provides interoperability between legacy APIs that return enumerations and new APIs that require collections. Syntax: public static ArrayList li
2 min read
Java.util.Collections.frequency() in Java
The method is a java.util.Collections class method. It counts the frequency of the specified element in the given list. It override the equals() method to perform the comparison to check if the specified Object and the Object in the list are equal or not. Syntax: public static int frequency(Collection c, Object o) parameters: c: Collection in which
2 min read
Java.util.Collections.rotate() Method in Java with Examples
java.util.Collections.rotate() method is present in java.util.Collections class. It is used to rotate the elements present in the specified list of Collection by a given distance. Syntax: public static void rotate(List&lt; type &gt; list, int distance) Parameters : list - the list to be rotated. distance - the distance to rotate the list. type - Ty
2 min read
Java.util.Collections.disjoint() Method in java with Examples
java.util.Collections.disjoint() method is present in java.util.Collections class. It is used to check whether two specified collections are disjoint or not. More formally, two collections are disjoint if they have no elements in common. Syntax: public static boolean disjoint(Collection&lt;?&gt; c1, Collection&lt;?&gt; c2) Parameters : c1 - a colle
3 min read
Java Program For Pairwise Swapping Elements Of A Given Linked List
Given a singly linked list, write a function to swap elements pairwise. Input: 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;6-&gt;NULL Output: 2-&gt;1-&gt;4-&gt;3-&gt;6-&gt;5-&gt;NULL Input: 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;NULL Output: 2-&gt;1-&gt;4-&gt;3-&gt;5-&gt;NULL Input: 1-&gt;NULL Output: 1-&gt;NULL For example, if the linked list is 1-&gt;2-&gt;3-&gt;4-&gt;
3 min read