How to Shuffle Deck of Cards in Java
-
Shuffle the
ArrayList
Using the Traditional Loop in Java -
Shuffle the
ArrayList
Using the Collectionsshuffle
Function in Java
The word shuffle literally means to arrange the elements, objects, or cards in random or undefined order. The objects list that does not follow a defined pattern is considered shuffled.
Similarly, in Java, various methods allow a user to shuffle elements. Below is the code block to demonstrate this process.
Shuffle the ArrayList
Using the Traditional Loop in Java
import java.util.ArrayList;
public class Main {
private static final int SIZE_OF_DECK = 52;
public static void main(String[] args) {
ArrayList<Integer> cardsDeck = new ArrayList<>();
for (int i = 0; i < SIZE_OF_DECK; ++i) {
cardsDeck.add(i);
}
System.out.println("Deck Of Cards:" + cardsDeck);
ArrayList<Integer> shuffledDeck = new ArrayList<>();
while (cardsDeck.size() > 0) {
int index = (int) (Math.random() * cardsDeck.size());
int removedCard = cardsDeck.remove(index);
shuffledDeck.add(removedCard);
}
System.out.println("Shuffled Cards" + shuffledDeck);
}
}
In the code above, an ArrayList
is instantiated. The instance created helps in adding elements into the ArrayList
. The list is filled with sequential order from 0
to 51
values. The deck size is a static variable declared at the class level.
The for
loop checks for the condition, wherein the initial value is checked with the static deck size variable. Now to shuffle the initial list, another empty ArrayList
is created. We will apply a while
loop with a condition where the deck size must be greater than zero value.
The idea to keep this condition is because the element from one deck is removed and placed in another shuffled list. Hence, the original size keeps on decreasing and finally becomes zero.
Within the loop, we use the random
function to find the index first. The static method is present in the Math
class and directly gets called by the class name. The utility function generates a random number ranging between 0.0
and 1.0
. The method always returns a double
value.
To calculate the index
value, the random number formed is multiplied by the current size of the array list. Hence, generating a random index each time.
Now, the index formed is used to remove the element at the index in the original deck. The remove
method is from the ArrayList
class, and it removes the particular value at the index. It takes the index or the position in the list to be removed.
The function also returns the element at the index in the list. The object to be removed gets added to the new shuffled list. The loop continues, where one element now reduces the size of the original deck. Hence, when it iterates for the next time, a new index will be generated. And each time, a value is removed and appended in the shuffled list.
Below is the output of the given code block.
Deck Of Cards:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51]
Shuffled Cards[51, 34, 28, 47, 14, 49, 42, 50, 26, 0, 44, 43, 2, 36, 30, 8, 46, 11, 21, 23, 7, 4, 33, 41, 32, 1, 20, 3, 10, 18, 6, 40, 29, 24, 31, 13, 45, 39, 22, 15, 27, 48, 9, 5, 25, 12, 38, 35, 37, 17, 16, 19]
Shuffle the ArrayList
Using the Collections shuffle
Function in Java
Below is the code block demonstrating the shuffling using the shuffle
method.
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
int deckSize = 52;
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < deckSize; ++i) {
list.add(i);
}
System.out.println("Sequential List: " + list);
Collections.shuffle(list);
System.out.println("Shuffled List: " + list);
}
}
In the code block, the procedure of ArrayList
instantiation is the same. The addition of elements in the list is done similarly to the one in the first code. We print the ordered list to the console using the println
method.
Now, the shuffle
function of Collections
is called. This method performs permutation and randomly computes the values in the given list. This function dumps the randomly selected element back into the list. It throws the UnsupportedOperationException
when the list provided does not support the set operation. The randomly formed list is printed in the console along with the original lists.
Sequential List[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51]
Shuffled List[46, 5, 4, 0, 19, 11, 13, 21, 1, 38, 36, 29, 31, 35, 48, 22, 49, 6, 14, 51, 3, 47, 16, 12, 42, 27, 50, 40, 26, 30, 33, 9, 43, 39, 2, 10, 28, 44, 8, 24, 41, 32, 25, 45, 34, 7, 23, 15, 18, 20, 37, 17]
Rashmi is a professional Software Developer with hands on over varied tech stack. She has been working on Java, Springboot, Microservices, Typescript, MySQL, Graphql and more. She loves to spread knowledge via her writings. She is keen taking up new things and adopt in her career.
LinkedInRelated Article - Java ArrayList
- How to Sort Objects in ArrayList by Date in Java
- How to Find Unique Values in Java ArrayList
- Vector and ArrayList in Java
- Differences Between List and Arraylist in Java
- How to Compare ArrayLists in Java
- Merge Sort Using ArrayList in Java
Related Article - Java Loop
- How to Use Index With forEach in Java
- How to Create Java Do-While Loop With User Input
- How to Implement Java while Loop With User Input
- How to Break Out of a for Loop in Java
- How to Break Nested Loops in Java