Site icon codippa

How to shuffle an ArrayList in java / Shuffle an ArrayList without Collections framework / Various ways to shuffle an ArrayList in java

Shuffling the contents of an ArrayList is a problem which can test the logic of a person and also demonstrates knowledge of java api. This post contains a couple of methods which shuffle the contents of ArrayList.

Method 1 : Without using Collections class

This method utilizes the approach of random number generation which is within the size of the list and then swapping the elements at two different indices of the list.

Algorithm
Logic to shuffle ArrayList without using Collections class involves the following steps :

  1. Calculate the size of the list.
  2. Iterate over the list and in each iteration :
    a. Retrieve the list element at current index using its get method.
    b. Generate a random number which lies with in the range of the size of the list using java.util.Random class.
  3. Get the list element at the random index generated above.
  4. Swap the elements at current loop index and random index generated above(Elements at step 2a and 3).

For generating Random number within a range, refer this post

Program implementing this logic along with sample output for different executions of the same program is given below.

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

public class ShuffleList {

    public static void main(String[] args) {
	List list = new ArrayList();
	list.add(1);
	list.add(2);
	list.add(3);
	list.add(4);
	list.add(5);
	list.add(6);
	list.add(7);
	System.out.println("Before shuffling : " + list);
	shuffle(list);
	System.out.println("After shuffling : " + list);
    }

    private static void shuffle(List list) {
	// get size of the list
	int totalElements = list.size();
	// initialize random number generator
	Random random = new Random();
	for (int loopCounter = 0; loopCounter < totalElements; loopCounter++) {
	   // get the list element at current index
	   int currentElement = list.get(loopCounter);
	   // generate a random index within the range of list size
	   int randomIndex = loopCounter + random.nextInt(totalElements - loopCounter);
	   // set the element at current index with the element at random
	   // generated index
	   list.set(loopCounter, list.get(randomIndex));
	   // set the element at random index with the element at current loop
	   // index
	   list.set(randomIndex, currentElement);
	}
   }
}
Output

Before shuffling : [1, 2, 3, 4, 5, 6, 7] After shuffling : [1, 2, 4, 5, 6, 3, 7] Before shuffling : [1, 2, 3, 4, 5, 6, 7] After shuffling : [5, 3, 7, 1, 2, 4, 6]

Method 2 : Using Collections class
java.util.Collections class has a shuffle method which takes a java.util.List as argument and rearranges the contents of the list.

shuffle method utilizes nextInt method of java.util.Random class to perform shuffling of elements.

 

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

public class ShuffleList {

     public static void main(String[] args) {
	List list = new ArrayList();
	list.add(1);
	list.add(2);
	list.add(3);
	list.add(4);
	list.add(5);
	list.add(6);
	list.add(7);
	System.out.println("Before shuffling : " + list);
	shuffleUsingCollections(list);
	System.out.println("After shuffling : " + list);
    }

    private static void shuffleUsingCollections(List list){
	Collections.shuffle(list);
    }
}
Output

Before shuffling : [1, 2, 3, 4, 5, 6, 7] After shuffling : [1, 6, 2, 4, 3, 7, 5] Before shuffling : [1, 2, 3, 4, 5, 6, 7] After shuffling : [7, 3, 5, 1, 6, 4, 2]

Thanks for reading this article. Hit the clap button below if you liked it!!!

Exit mobile version