How to Shuffle Elements in LinkedList in Java?
Last Updated :
04 Jan, 2021
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[])
{
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 list before shuffle : "
+ list.toString());
System.out.println();
Object[] array = list.toArray();
arrayShuffle(array);
listDataAdder(array, list);
System.out.println( "The list after shuffle 1 : "
+ list.toString());
System.out.println();
arrayShuffle(array);
listDataAdder(array, list);
System.out.println( "The list after shuffle 2 : "
+ list.toString());
System.out.println();
arrayShuffle(array);
listDataAdder(array, list);
System.out.println( "The list after shuffle 3 : "
+ list.toString());
}
static void listDataAdder(Object[] arr,
LinkedList<Object> list)
{
ListIterator<Object> it = list.listIterator();
for (Object e : arr) {
it.next();
it.set(e);
}
}
static void arrayShuffle(Object[] arr)
{
Random rand = new Random();
for ( int i = 0 ; i < arr.length - 1 ; i++) {
int index = rand.nextInt(i + 1 );
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[])
{
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();
Collections.shuffle(list);
System.out.println(
"The LinkedList after shuffle 1 : "
+ list.toString());
System.out.println();
Collections.shuffle(list);
System.out.println(
"The LinkedList after shuffle 2 : "
+ list.toString());
System.out.println();
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]
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...