Skip to content

Commit d6407ef

Browse files
harshilsharma63pivovarit
authored andcommitted
BAEL-1539 shuffling collections (eugenp#3701)
* 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 * #BAEL-1539 updated map shuffling logic to shuffle entryset instead of keyset
1 parent b35b913 commit d6407ef

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

‎core-java-8/src/test/java/com/baeldung/shufflingcollections/ShufflingCollectionsUnitTest.java‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public void whenShufflingList_thenListIsShuffled() {
2222
}
2323

2424
@Test
25-
public void whenShufflingMapKeys_thenValuesAreShuffled() {
25+
public void whenShufflingMapEntries_thenValuesAreShuffled() {
2626
Map<Integer, String> studentsById = new HashMap<>();
2727
studentsById.put(1, "Foo");
2828
studentsById.put(2, "Bar");
@@ -32,12 +32,12 @@ public void whenShufflingMapKeys_thenValuesAreShuffled() {
3232
System.out.println("Students before shuffling:");
3333
System.out.println(studentsById.values());
3434

35-
List<Integer> shuffledStudentIds = new ArrayList<>(studentsById.keySet());
36-
Collections.shuffle(shuffledStudentIds);
35+
List<Map.Entry<Integer, String>> shuffledStudentEntries = new ArrayList<>(studentsById.entrySet());
36+
Collections.shuffle(shuffledStudentEntries);
3737

38-
List<String> shuffledStudents = shuffledStudentIds.stream()
39-
.map(id -> studentsById.get(id))
40-
.collect(Collectors.toList());
38+
List<String> shuffledStudents = shuffledStudentEntries.stream()
39+
.map(Map.Entry::getValue)
40+
.collect(Collectors.toList());
4141

4242
System.out.println("Students after shuffling");
4343
System.out.println(shuffledStudents);

0 commit comments

Comments
 (0)