Home  >  Core Java

Java Collections shuffle() and copy() Example

By Arvind Rai, July 09, 2016
On this page we will provide java Collections shuffle() and copy() method example. Collections.shuffle() is used to permutate the specified list and Collections.copy() is used to copy java list from source to destination list. Find the example.

Collections.shuffle

shuffle() randomly achieves permutation for the specified list using a default randomness. This method accepts java List as an argument. If we do not want default randomness, we can also provide our Random instance as an argument. Find the sample example.
CollectionsShuffle.java
package com.concretepage.util;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class CollectionsShuffle {
    public static void main(String[] args) {
        User u1 = new User("Ramesh");
        User u2 = new User("Mahesh");
        User u3 = new User("Suresh");
        User u4 = new User("Nilesh");
        List<User> list = Arrays.asList(u1,u2,u3,u4);
        System.out.println("...before shuffle....");
        for(User u: list){
            System.out.print(u.getName()+" ");
        }
        System.out.println("\n...after shuffle....");
        Collections.shuffle(list);
        for(User u: list){
            System.out.print(u.getName()+" ");
        }
        System.out.println("\n...after re-shuffle....");
        Collections.shuffle(list);
        for(User u: list){
            System.out.print(u.getName()+" ");
        }        
    }
} 
Output
...before shuffle....
Ramesh Mahesh Suresh Nilesh 
...after shuffle....
Mahesh Nilesh Ramesh Suresh 
...after re-shuffle....
Suresh Mahesh Nilesh Ramesh  



Collections.copy

copy() method copies the java List instance from source to destination. It copies the elements index wise. In the destination list, elements are copied at the same indices as the elements are in source list. If elements are already there in destination list, it will be overridden by source list. If destination list size is larger than source list, Collections.copy method will copy the source list from index 0 in destination list and after copying the source list, the other elements of the destination list will remain as before.
Find the example for the scenario when destination list size is equal or lesser than source list.
CollectionsCopyOne.java
package com.concretepage.util;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class CollectionsCopyOne {
    public static void main(String[] args) {
    	List<Integer> source = Arrays.asList(1,2,3);
    	List<Integer> dest = Arrays.asList(4,5,6);
    	Collections.copy(dest, source);
    	for(Integer i : dest) {
    		System.out.print(i +" ");
    	}
    }
} 
Output
1 2 3 
 
Now find the example for the scenario when destination list size is larger than source list. CollectionsCopyTwo.java
package com.concretepage.util;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class CollectionsCopyTwo {
    public static void main(String[] args) {
    	List<Integer> source = Arrays.asList(1,2,3,4);
    	List<Integer> dest = Arrays.asList(5,6,7,8,9,10);
    	Collections.copy(dest, source);
    	for(Integer i : dest) {
    		System.out.print(i +" ");
    	}
    }
} 
Output
1 2 3 4 9 10 
 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us