From d6407ef405e2a2568cb48beb56e998d3b37070d0 Mon Sep 17 00:00:00 2001 From: Harshil Sharma Date: Wed, 21 Feb 2018 16:16:15 +0530 Subject: [PATCH] BAEL-1539 shuffling collections (#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 --- .../ShufflingCollectionsUnitTest.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/core-java-8/src/test/java/com/baeldung/shufflingcollections/ShufflingCollectionsUnitTest.java b/core-java-8/src/test/java/com/baeldung/shufflingcollections/ShufflingCollectionsUnitTest.java index 4823c7178a..d013907c9a 100644 --- a/core-java-8/src/test/java/com/baeldung/shufflingcollections/ShufflingCollectionsUnitTest.java +++ b/core-java-8/src/test/java/com/baeldung/shufflingcollections/ShufflingCollectionsUnitTest.java @@ -22,7 +22,7 @@ public class ShufflingCollectionsUnitTest { } @Test - public void whenShufflingMapKeys_thenValuesAreShuffled() { + public void whenShufflingMapEntries_thenValuesAreShuffled() { Map studentsById = new HashMap<>(); studentsById.put(1, "Foo"); studentsById.put(2, "Bar"); @@ -32,12 +32,12 @@ public class ShufflingCollectionsUnitTest { System.out.println("Students before shuffling:"); System.out.println(studentsById.values()); - List shuffledStudentIds = new ArrayList<>(studentsById.keySet()); - Collections.shuffle(shuffledStudentIds); + List> shuffledStudentEntries = new ArrayList<>(studentsById.entrySet()); + Collections.shuffle(shuffledStudentEntries); - List shuffledStudents = shuffledStudentIds.stream() - .map(id -> studentsById.get(id)) - .collect(Collectors.toList()); + List shuffledStudents = shuffledStudentEntries.stream() + .map(Map.Entry::getValue) + .collect(Collectors.toList()); System.out.println("Students after shuffling"); System.out.println(shuffledStudents);