Java 8 how to shuffle several ArrayList in same order

If you want to shuffle two and more list in Java 8 you can do it by using Random and seed. Three examples will show how to do it.

You may want to check:

Shuffling two ArrayList-s of strings in same order

Shuffling two list in Java can be done by using seed and random. You need to generate Random object and to provide it with seed. In the example below you can see it:

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class Test {

    public static void main(String[] args) {

        List<String> alist = Arrays.asList("A1",	"A2",	"A3",	"A4",	"A5",	"A6",	"A7");
        List<String> blist = Arrays.asList("B1",	"B2",	"B3",	"B4",	"B5",	"B6",	"B7");

        Random r = new Random();
        long seed = r.nextLong();

        r.setSeed(seed);
        Collections.shuffle(alist, r);

        r.setSeed(seed);
        Collections.shuffle(blist, r);

        printArrayList(alist);
        printArrayList(blist);
    }

    //
    // print ArrayList comma separated
    //
    public static final void printArrayList(List<String> list) {
        for (String temp : list) {
            System.out.print(temp + ", ");
        }
        System.out.println();
    }
}

result:

A1, A2, A6, A3, A5, A7, A4,
B1, B2, B6, B3, B5, B7, B4,

Shuffling N ArrayList-s in same order

If you want to shuffle more than 2 list in the same order you can apply the same logic. Example how to shuffle 7 ArrayList in the same order working with java 8 and more:

import java.util.*;

import static java.util.stream.Collectors.toCollection;
import static java.util.stream.Collectors.toList;

public class ShuffleSameOrder {
    public static void main(String[] args) {
        long seed = System.nanoTime();
        cloneCollect("copy", seed);
        cloneCollect("asList", seed);
        cloneCollect("toList", seed);
        cloneCollect("toCollection", seed);
    }

    public static final void cloneCollect(String expression, long seed){

        Random r = new Random();
        System.out.println("-------" + expression +  "-------");
        List<String> copy = Arrays.asList("A1",	"A2",	"A3",	"A4",	"A5",	"A6",	"A7");
        for (int i = 0; i < copy.size(); i++)   {
            List<String> copy1;
            switch(expression) {
                case "copy" :
                    copy1 = copy;
                    break; // optional
                case "asList" :
                    copy1 = Arrays.asList("A1",	"A2",	"A3",	"A4",	"A5",	"A6",	"A7");
                    break; // optional
                case "toList" :
                    copy1 = copy.stream().map(String::new).collect(toList());
                    break; // optional
                case "toCollection" :
                    copy1 = copy.stream().map(String::new).collect(toCollection(ArrayList::new));
                    break; // optional
                // You can have any number of case statements.
                default : // Optional
                    copy1 = cloneArrayList(copy);
            }
            r.setSeed(seed);
            Collections.shuffle(copy1, r);
            printArrayList(copy1);
        }
    }
    //
    // clone ArrayList
    //
    public static final ArrayList<String> cloneArrayList(List<String> list) {
        return list.stream().map(String::new).collect(toCollection(ArrayList::new));
    }
    //
    // print ArrayList comma separated
    //
    public static final void printArrayList(List<String> list) {
        for (String temp : list) {
            System.out.print(temp + ", ");
        }
        System.out.println();
    }
}

result:

-------copy-------
A7, A4, A6, A3, A1, A2, A5, 
A5, A3, A2, A6, A7, A4, A1, 
A1, A6, A4, A2, A5, A3, A7, 
A7, A2, A3, A4, A1, A6, A5, 
A5, A4, A6, A3, A7, A2, A1, 
A1, A3, A2, A6, A5, A4, A7, 
A7, A6, A4, A2, A1, A3, A5, 
-------asList-------
...  
A7, A4, A6, A3, A1, A2, A5, 
A7, A4, A6, A3, A1, A2, A5, 
-------toList-------
...  
A7, A4, A6, A3, A1, A2, A5, 
A7, A4, A6, A3, A1, A2, A5, 
-------toCollection-------
... 
A7, A4, A6, A3, A1, A2, A5, 
A7, A4, A6, A3, A1, A2, A5, 

Shuffling two ArrayList-s of integers in same order

Another example showing how to shuffle Students and Questions ids from two ArrayList-s in same order:


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

public class TestShuffle {

    public static void main(String[] args) {
        Random rnd = new Random();
        long seed = rnd.nextLong();

        List<Integer> student_list = new ArrayList<Integer>();
        student_list.add(91);
        student_list.add(52);
        student_list.add(34);
        student_list.add(65);

        List<Integer> question_list = new ArrayList<Integer>();
        question_list.add(91);
        question_list.add(52);
        question_list.add(34);
        question_list.add(65);

        rnd.setSeed(seed);
        Collections.shuffle(student_list, rnd);
        rnd.setSeed(seed);
        Collections.shuffle(question_list, rnd);

        printArrayList(student_list);
        printArrayList(question_list);
    }
    
    public static final void printArrayList(List<Integer> list) {
        for (Integer temp : list) {
            System.out.print(temp + ", ");
        }
        System.out.println();
    }

}

result:

91, 65, 34, 52,
91, 65, 34, 52,

Related Article