
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Shuffling Elements of Unordered Collections in Java
There are two types of Collections in Java. One is ordered and the other one is unordered collection. The ordered collections store their elements in the order in which they are inserted i.e. it maintains the insertion order of elements. Whereas, the unordered collections such as Map and Set do not maintain any order.
In this article, we will create an unordered collection and try to shuffle its elements using the inbuilt method Collections.shuffle().
The Collections.shuffle() Method
The Collections.shuffle() method is provided by java.util package. It takes a list as an argument and then rearranges the elements randomly.
Therefore, before shuffling the Map and Set interfaces, we need to convert them in list.
Syntax
Use the given syntax of this method in your Java program:
Collections.shuffle( nameOfcollection );
Shuffle Elements of Unordered Collection Set
Set is an unordered collection that cannot store duplicate elements. It extends Collection Interface and inherits its methods. To use the features of Set, we will use TreeSet class that implements Set interface.
Syntax to define a TreeSet:
Set< element_Type > collection_name = new TreeSet<>();
Here, element_Type is the wrapper class not primitive datatypes.
Example
In this Java program, we will create a ArrayList and copy all the elements of the previous Tree Set in it. Then, we shuffle the elements using the Collections.shuffle() method.
import java.util.*; public class Srtset { public static void main(String args[]) { // Creating a tree set Set<String> treeSt = new TreeSet<>(); // Adding elements in the tree set treeSt.add("Tutorix"); treeSt.add("Simply"); treeSt.add("Easy"); treeSt.add("Learning"); treeSt.add("Tutorials"); treeSt.add("Point"); // print elements before shuffling System.out.println("Elements of the given set without performing shuffling: "); System.out.println(treeSt); // storing the elements of tree set in array list List<String> arayList = new ArrayList<>(treeSt); // performing shuffle operation on list Collections.shuffle(arayList); // display the shuffled elements System.out.println("Shuffled elements of the given set: "); System.out.println(arayList); } }
Compile and execute the code to get the result.
Elements of the given set without performing shuffling: [Easy, Learning, Point, Simply, Tutorials, Tutorix] Shuffled elements of the given set: [Easy, Simply, Learning, Tutorix, Tutorials, Point]
Shuffle elements of Unordered Collection Map
Map is also an unordered collection that maps unique keys to values. To work with this interface, we use the TreeMap class. It stores the elements of the map in a tree structure.
The syntax for TreeMap is as follows:
TreeMap< TypeOfKey, TypeOfValue > nameOfMap = new TreeMap<>();
Example
In the following Java program, we copy all the elements of TreeMap using entrySet() method. Then, we use the Collections.shuffle() method to shuffle the elements.
import java.util.*; public class Suffle { public static void main(String args[]) { TreeMap<String, Integer> workers = new TreeMap<>(); // Adding elements in the workers map workers.put("Vaibhav", 4000); workers.put("Ansh", 3000); workers.put("Vivek", 1500); workers.put("Aman", 2000); workers.put("Tapas", 2500); // printing details workers map without shuffle System.out.println("Elements of the map: "); for (String unKey : workers.keySet()) { System.out.println("Name: " + unKey + ", Salary: " + workers.get(unKey)); } // create new ArrayList List<Map.Entry<String, Integer>> arayList = new ArrayList<>(workers.entrySet()); Collections.shuffle(arayList); // printing details after shuffling System.out.println("Elements of the newly shuffled map: "); for (Map.Entry<String, Integer> print : arayList) { System.out.println("Name: " + print.getKey() + ", Salary: " + print.getValue()); } } }
When you run the code, you will see elements before and after shuffling:
Elements of the map: Name: Aman, Salary: 2000 Name: Ansh, Salary: 3000 Name: Tapas, Salary: 2500 Name: Vaibhav, Salary: 4000 Name: Vivek, Salary: 1500 Elements of the newly shuffled map: Name: Vaibhav, Salary: 4000 Name: Aman, Salary: 2000 Name: Vivek, Salary: 1500 Name: Ansh, Salary: 3000 Name: Tapas, Salary: 2500
Conclusion
In this article, we have learned how we can shuffle the elements of an unordered collection with the help of examples. We also discovered two of the unordered collections named Map and Set.