Signup/Sign In

How to Create a Swap Function in Java

In this article, I will walk you through some examples of swapping primitive and no primitive data-types. Comparatively, on primitive data-types, we can easily perform swapping but in the case of non-primitive data-types, it is very intricate work. java.util.Collections class supports swap() which is used to swap the elements from a list of primitive and non-primitive data-type.

Before moving ahead with the swap() method let's look at the classic example of swapping of two variables.

Example of swapping two variables

This is the conventional way of swapping two variables. Let's say we have two variables a and b then the value of a will be assigned to b, similarly the value of b to the value of a. One variable can hold one value at a time that's why we created temp variable which will hold value for temporary value. You can observe from the output value of both the variable is changing.

public class StudyTonight 
	public static void main(String[] args)   
		int a=10;
		int b=20;
		int temp;
		System.out.println("Before Swap: a="+a+" b:"+b);
		System.out.println("After Swap: a="+a+" b:"+b);

Before Swap: a=10 b:20
After Swap: a=20 b:10

We can see the above code will work finely for swapping of two variables but it takes extra memory. To save memory we can use the swapping formula:




These methods are good when we want to work on two elements. What if there is a necessity to make swapping on the list of objects.

In such cases Collections.swap() function is very helpful:

public static void swap(List<?> list, int indexOne, int indexTwo)  

list: It is the list on which we want to perform swapping

indexOne, indexTwo: These are the index in the list which will be swapped by function

enlightenedThis method throws an exception IndexOutOfBoundException if any of the indexes cross the bound of the list.

Example of swap of elements in the list using swap() function

In the below example, we made an ArrayList of String and after that, we performed a swap operation using Collections,swap() method on 0th and 2nd elements. Swapping of elements can be easily noticed by comparing lists before and after swapping.

Before Swapping: [Bangalore, Delhi, Noida, Mumbai]

After Swapping: [Noida, Delhi, Bangalore, Mumbai]

import java.util.*;  
public class StudyTonight 
	public static void main(String[] args)   
		ArrayList<String>  list = new ArrayList<String>();  
		System.out.println("List Before Swapping : " +list);   
		Collections.swap(list, 0, 2);  
		System.out.println("List after swapping : " +list);  

List Before Swapping : [Bangalore, Delhi, Noida, Mumbai]
List after swapping : [Noida, Delhi, Bangalore, Mumbai]


To swap primitive elements we can simply use one temp variable and swap the value. But in the case of a list of objects, we may end up using a lot of extra space. In those cases, it is very beneficial to use Collections.swap() method for swapping elements in the list. we can perform this operation as many times as we want. We just need to take care of IndexOutOfBoundException. This method is most useful when we work on a list of large objects to save the extra overhead of creating temporary objects.

About the author:
I am a 3rd-year Computer Science Engineering student at Vellore Institute of Technology. I like to play around with new technologies and love to code.