Thursday 2 July 2015

Java : Collection Framework : Collections (Shuffle Random)


Click here to watch in Youtube : 
https://www.youtube.com/watch?v=iC_41E2XwHo&list=UUhwKlOVR041tngjerWxVccw

CollectionsExample.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

/*
 Method: 

 public static void shuffle(List<?> list,Random rnd)

 Parameters:
 
 list - the list to be shuffled.
 rnd - the source of randomness to use to shuffle the list.

 */

public class CollectionsExample
{

    public static void main(String[] args)
    {

        ArrayList<Integer> arrayList = new ArrayList<Integer>();

        arrayList.add(1000);
        arrayList.add(2000);
        arrayList.add(3000);
        arrayList.add(4000);
        arrayList.add(5000);

        System.out.println("Before shuffle , arrayList : " + arrayList + "\n");

        /*
         * Randomly permute the specified list using the specified source of
         * randomness.
         */
        Collections.shuffle(arrayList, new Random());

        System.out.println("After shuffle , arrayList : " + arrayList + "\n");

    }

}
Output
Before shuffle , arrayList : [1000, 2000, 3000, 4000, 5000]

After shuffle , arrayList : [1000, 5000, 2000, 3000, 4000]
To Download CollectionsDemoShuffleRandom Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/CollectionsDemoShuffleRandom.zip?attredirects=0&d=1

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment