Skip to content

Commit 666c07c

Browse files
harshilsharma63KevinGilmore
authored andcommitted
BAEL-1539 shuffling collections (eugenp#3567)
* BAEL-1539 Added list, set and map shuffling code exaamples * #BAEL-1539 fixed a typo * #BAEL-1539 refactored sample code, added unit tests * #BAEL-1539 Added unit tests and example for shuffling with custom randomness * #BAEL-1539 removed source code and kept only tests as tests are sufficient code sample themselves * #BAEL-1539 updated map shuffling example to use lambdas * #BAEL-1539 lambda refactoring * Fixed an indentation
1 parent 25424df commit 666c07c

1 file changed

Lines changed: 70 additions & 0 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.baeldung.shufflingcollections;
2+
3+
import org.junit.Test;
4+
5+
import java.util.*;
6+
import java.util.stream.Collectors;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
public class ShufflingCollectionsUnitTest {
11+
12+
@Test
13+
public void whenShufflingList_thenListIsShuffled() {
14+
List<String> students = Arrays.asList("Foo", "Bar", "Baz", "Qux");
15+
16+
System.out.println("List before shuffling:");
17+
System.out.println(students);
18+
19+
Collections.shuffle(students);
20+
System.out.println("List after shuffling:");
21+
System.out.println(students);
22+
}
23+
24+
@Test
25+
public void whenShufflingMapKeys_thenValuesAreShuffled() {
26+
Map<Integer, String> studentsById = new HashMap<>();
27+
studentsById.put(1, "Foo");
28+
studentsById.put(2, "Bar");
29+
studentsById.put(3, "Baz");
30+
studentsById.put(4, "Qux");
31+
32+
System.out.println("Students before shuffling:");
33+
System.out.println(studentsById.values());
34+
35+
List<Integer> shuffledStudentIds = new ArrayList<>(studentsById.keySet());
36+
Collections.shuffle(shuffledStudentIds);
37+
38+
List<String> shuffledStudents = shuffledStudentIds.stream()
39+
.map(id -> studentsById.get(id))
40+
.collect(Collectors.toList());
41+
42+
System.out.println("Students after shuffling");
43+
System.out.println(shuffledStudents);
44+
}
45+
46+
@Test
47+
public void whenShufflingSet_thenElementsAreShuffled() {
48+
Set<String> students = new HashSet<>(Arrays.asList("Foo", "Bar", "Baz", "Qux"));
49+
50+
System.out.println("Set before shuffling:");
51+
System.out.println(students);
52+
53+
List<String> studentList = new ArrayList<>(students);
54+
55+
Collections.shuffle(studentList);
56+
System.out.println("Shuffled set elements:");
57+
System.out.println(studentList);
58+
}
59+
60+
@Test
61+
public void whenShufflingWithSameRandomness_thenElementsAreShuffledDeterministically() {
62+
List<String> students_1 = Arrays.asList("Foo", "Bar", "Baz", "Qux");
63+
List<String> students_2 = Arrays.asList("Foo", "Bar", "Baz", "Qux");
64+
65+
Collections.shuffle(students_1, new Random(5));
66+
Collections.shuffle(students_2, new Random(5));
67+
68+
assertThat(students_1).isEqualTo(students_2);
69+
}
70+
}

0 commit comments

Comments
 (0)