Want to write articles/new answer to be published in javapedia.net? Send in your article with your name to javatutorials2016@gmail.com!

Prev Next

Java / Collections

How do you swap two elements in a list using Collections class API?

Using Collections.swap.

It swaps the elements at the specified positions in a specified list.

  swap(List listElement, int i, int k)

This static method of Collections class swap the element between the position i and k at the listElement.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CollectionsSwap {

	public static void main(String[] args) {

		List<String> fruitList = Arrays.asList("Apple", "Orange", "Strawberry");

				.println("The fruits available in the shopping List based on priority:");
		for (String fruit : fruitList) {
			System.out.println("* " + fruit);

		// Swapping the priority between Orange and apple
		Collections.swap(fruitList, 0, 1);

				.println("The fruits available in the shopping List (priority changed):");
		for (String fruit : fruitList) {
			System.out.println("* " + fruit);



❤Cash Back At Stores you Love !!!❤

Earn your $10 reward when you make your first purchase through Ebates by signing up with clicking below button.

Ebates Coupons and Cash Back

More Related questions...

How to reverse the List using Collections? What get() method return if the key does not exist at HashMap? How get() method will handle if two keys have the same hashcode? Explain get(keyObj) method of HashMap/Hashtable. How do I parse an URL String into Name-Value Collection? How do you initialize an ArrayList? What is Java Collections Framework? Advantages of using Collections Framework. Root interface in collection hierarchy. Where does the Java Collection framework packaged? How do I synchronize a ArrayList In Java? Does HashMap get() method work with value object's hashcode? What is Entry? Difference between poll() and remove() method of Queue interface. Difference between fail-fast and fail-safe Iterators. Which collection classes are synchronized or thread-safe ? What are the core Collection interfaces? What is the difference between List and Set ? What is the difference between Map and Set ? What are the classes that implements List and Set interface ? What is an iterator? What is the difference between Iterator and Enumeration ? Which design pattern implemented by Iterator? What are the methods to be overridden to use an object type as key in HashMap ? What is the difference between Queue and Stack? Convert the array of strings into a list. What is the difference between HashMap and Hashtable? What is the difference between peek(),poll() and remove() method of the Queue interface? What is the difference between Iterator and ListIterator? What is the difference between Array and ArrayList in Java? How do you swap two elements in a list using Collections class API? Does HashSet ignore String case when contains() method is invoked in Java? What is IdentityHashmap in Java? What are the similarities between HashSet, LinkedHashSet and TreeSet In Java? List the differences between LinkedList and ArrayList in Java. List out the similarities between ArrayList and LinkedList in Java. Java: Which algorithm does Collections.sort() use? Why Collection does not extend Cloneable and Serializable interfaces ? Difference between Comparable and Comparator interface. Difference between Synchronized Collection and Concurrent Collection. What is BlockingQueue in Java collections? Difference between the add and offer methods in a Queue in Java. How do you filter a Java Collection? How to avoid ConcurrentModificationException when removing while Iterating through a Collection? What is the difference between Streams and Collections in Java 8? What is predicate in Java 8? Advantages of using Lambda expression. Difference between Predicate and function in Java 8. Difference between IntStream.rangeClosed() and range() in Java 8. What is the main objective of streaming and lambda in Java8? How are the parallel streams implemented in Java8? Explain Supplier interface in Java8. What is BooleanSupplier interface in Java8? Why do we need to use Java 8 Stream API? The default capacity of Collection Elements: ArrayList, Vector, HashSet, Hashtable, and HashMap. How Fail Fast Iterator works internally?
Show more question and Answers...

List and its implementations

Comments & Discussions