Home » Java programming language

Java Collections shuffle() Method with Example

Collections Class shuffle() method: Here, we are going to learn about the shuffle() method of Collections Class with its syntax and example.
Submitted by Preeti Jain, on February 08, 2020

Collections Class shuffle() method

Syntax:

    public static void shuffle(List l);
    public static void shuffle(List l, Random ran);
  • shuffle() method is available in java.util package.
  • shuffle(List l) method is used to shuffle elements of the given list randomly by default.
  • shuffle(List l, Random ran) method is used to shuffle elements of the given list by using the given Random (ran).
  • These methods may throw an exception at the time of shuffling elements of the list.
    UnsupportedOperationException: This exception may throw when the given parameter List (l) or its iterator un-support set operation.
  • These are static methods and it is accessible with the class name and if we try to access these methods with the class object then also we will not get any error.

Parameter(s):

  • In the first case, shuffle(List l),
    • List l – represents the list that is for shuffling.
  • In the first case, shuffle(List l, Random ran),
    • List l – represents the list that is for shuffling.
    • Random ran – represents the direction of randomness of the given list.

Return value:

In both the cases, the return type of the method is void, it does not return anything.

Example:

// Java program to demonstrate the example 
// of shuffle() method of Collections

import java.util.*;

public class ShuffleOfCollections {
    public static void main(String args[]) {
        // Instantiates an array list object
        List < Integer > arr_l = new ArrayList < Integer > ();
        Random ran = new Random();

        // By using add() method is to add
        // objects in an array list
        arr_l.add(20);
        arr_l.add(10);
        arr_l.add(40);
        arr_l.add(30);
        arr_l.add(50);

        // Display ArrayList
        System.out.println("ArrayList: " + arr_l);

        // By using shuffle(arr_l) method is to shuffle
        // the elements of arr_l bydefault
        Collections.shuffle(arr_l);

        //Display Shuffle ArrayList
        System.out.println("Collections.shuffle(arr_l): " + arr_l);

        // By using shuffle(arr_l,ran) method is to shuffle
        // the elements of arr_l based on the defined randomness
        Collections.shuffle(arr_l, ran);


        // Display Shuffle ArrayList
        System.out.println("Collections.shuffle(arr_l,ran) :" + arr_l);
    }
}

Output

ArrayList: [20, 10, 40, 30, 50]
Collections.shuffle(arr_l): [40, 50, 10, 30, 20]
Collections.shuffle(arr_l,ran) :[20, 30, 50, 10, 40]






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.