Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaHow to Shuffle Elements in LinkedList in Java?

How to Shuffle Elements in LinkedList in Java?

LinkedList is a part of the Collection framework present in java.util package. This class is an implementation of the LinkedList data structure which is a linear data structure where the elements are not stored in contiguous locations and every element is a separate object with a data part and address part. The elements are linked using pointers and addresses. Each element is known as a node. Given a LinkedList, the task is to shuffle the LinkedList. These are approaches to solve the problem.

Approach 1 (Using user-define method)

  • Create a LinkedList.
  • Store its elements in an array by the toArray() method. 
  • Shuffle the array elements.
  • Use ListIterator on the LinkedList and traverse the LinkedList by next() method and store the shuffled data of the Array to the List simultaneously by set() method.

Example

Java




import java.util.*;
public class GFG {
  
    public static void main(String args[])
    {
  
        // creating an instance of LinkedList
        LinkedList<Object> list = new LinkedList<>();
  
        // adding data to the LinkedList
        list.add(45);
        list.add("GFG");
        list.add(2.56f);
        list.add(965.321);
        list.add('A');
        list.add(true);
  
        System.out.println("The list before shuffle : "
                           + list.toString());
        System.out.println();
  
        // creating an Array and storing all data of the
        // list to the array
        Object[] array = list.toArray();
  
        // here we are shuffling more than once
        // shuffle 1
        arrayShuffle(array);
        listDataAdder(array, list);
        System.out.println("The list after shuffle 1 : "
                           + list.toString());
        System.out.println();
  
        // shuffle 2
        arrayShuffle(array);
        listDataAdder(array, list);
        System.out.println("The list after shuffle 2 : "
                           + list.toString());
        System.out.println();
  
        // shuffle 3
        arrayShuffle(array);
        listDataAdder(array, list);
        System.out.println("The list after shuffle 3 : "
                           + list.toString());
    }
  
    // Creating a data to add shuffled data of the array to
    // the list
    static void listDataAdder(Object[] arr,
                              LinkedList<Object> list)
    {
        // creating a ListIterator on the LinkedList
        ListIterator<Object> it = list.listIterator();
  
        // Traversing the LinkedList and setting all
        // shuffled data of the array to the list
        for (Object e : arr) {
            it.next();
            it.set(e);
        }
    }
  
    // creating a method to shuffle the array
    static void arrayShuffle(Object[] arr)
    {
        Random rand = new Random();
        for (int i = 0; i < arr.length - 1; i++) {
            
            // select index randomly
            int index = rand.nextInt(i + 1);
            
            // swapping between i th term and the index th
            // term
            Object g = arr[index];
            arr[index] = arr[i];
            arr[i] = g;
        }
    }
}


Output

The list before shuffle : [45, GFG, 2.56, 965.321, A, true]

The list after shuffle 1 : [965.321, 2.56, GFG, 45, A, true]

The list after shuffle 2 : [965.321, 45, A, 2.56, GFG, true]

The list after shuffle 3 : [965.321, 45, GFG, 2.56, A, true]

Approach 2(Using Collections.shuffle(list))

  • Create a LinkedList.
  • Shuffle the elements of the list by using the shuffle() method of the Collections class.

Declaration

public static void shuffle(List<?> list)

Syntax

Collections.shuffle(list);

Exception: This method throws UnsupportedOperationException if the given list or its list-iterator does not support the set operation.

Example:

Java




import java.util.Collections;
import java.util.LinkedList;
  
public class GFG {
    public static void main(String args[])
    {
        // Creating a LinkedList
        LinkedList<Object> list = new LinkedList<>();
        list.add(45);
        list.add("GFG");
        list.add(2.56f);
        list.add(965.321);
        list.add('A');
        list.add(true);
  
        System.out.println(
            "The LinkedList before shuffle : "
            + list.toString());
        System.out.println();
  
        // here we are shuffling more than once
        // shuffle 1
        Collections.shuffle(list);
        System.out.println(
            "The LinkedList after shuffle 1 : "
            + list.toString());
        System.out.println();
  
        // shuffle 2
        Collections.shuffle(list);
        System.out.println(
            "The LinkedList after shuffle 2 : "
            + list.toString());
        System.out.println();
  
        // shuffle 3
        Collections.shuffle(list);
        System.out.println(
            "The LinkedList after shuffle 3 : "
            + list.toString());
    }
}


Output

The LinkedList before shuffle : [45, GFG, 2.56, 965.321, A, true]

The LinkedList after shuffle 1 : [965.321, 2.56, true, 45, GFG, A]

The LinkedList after shuffle 2 : [2.56, A, true, 45, GFG, 965.321]

The LinkedList after shuffle 3 : [GFG, A, 965.321, 45, true, 2.56]
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments