- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Swapping items of a list in Java _ Collections.swap() with Example
This article will provide an in-depth explanation of how to swap items in a list using the Collections.swap() method in Java. This topic is crucial for anyone looking to strengthen their data manipulation skills in Java, especially in dealing with list data structures.
Java provides an extensive suite of tools for manipulating collections, including lists. One of these tools is the Collections.swap() method, which allows developers to easily swap the positions of two elements in a list. This method can be extremely handy when you need to rearrange elements in a list for various purposes, such as sorting, shuffling, or custom modifications.
Method Signature
The Collections.swap() method comes with the following signature −
public static void swap(List<?> list, int i, int j)
It takes three parameters −
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.
Usage
The Collections.swap() method is straightforward to use. It directly modifies the list passed to it, swapping the elements at the specified indices.
Example
Let's demonstrate this with a simple Java code snippet −
import java.util.*; public class Main { public static void main(String[] args) { List<String> list = new ArrayList<>(Arrays.asList("Hello", "world", "this", "is", "Java")); System.out.println("List before swapping: " + list); Collections.swap(list, 0, 4); System.out.println("List after swapping: " + list); } }
Output
The output of the above code is −
List before swapping: [Hello, world, this, is, Java] List after swapping: [Java, world, this, is, Hello]
As you can see, the words "Hello" and "Java" have been swapped in the list.
Explanation
In the above code, we've created a list of strings: ["Hello", "world", "this", "is", "Java"]. We then print this list to the console.
Next, we use the Collections.swap() method to swap the elements at the first (0) and last (4) positions in the list.
Finally, we print the list again to show the result of the swap operation.
Points to Remember
While the Collections.swap() method is a powerful tool, it's important to remember that it directly modifies the list it's given. This means you should be careful when using it on lists that shouldn't be changed, or when multiple threads are interacting with the same list. In such cases, it might be necessary to create a copy of the list or use synchronization to ensure correct behavior.
Conclusion
The Collections.swap() method in Java is an efficient and convenient way to swap elements in a list. This tool is not only useful for tasks like sorting and shuffling but also invaluable for custom data manipulation tasks in a wide range of applications.