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
This commit is contained in:
Harshil Sharma 2018-02-21 16:16:15 +05:30 committed by Grzegorz Piwowarek
parent b35b913cbe
commit d6407ef405
1 changed files with 6 additions and 6 deletions

View File

@ -22,7 +22,7 @@ public class ShufflingCollectionsUnitTest {
} }
@Test @Test
public void whenShufflingMapKeys_thenValuesAreShuffled() { public void whenShufflingMapEntries_thenValuesAreShuffled() {
Map<Integer, String> studentsById = new HashMap<>(); Map<Integer, String> studentsById = new HashMap<>();
studentsById.put(1, "Foo"); studentsById.put(1, "Foo");
studentsById.put(2, "Bar"); studentsById.put(2, "Bar");
@ -32,12 +32,12 @@ public class ShufflingCollectionsUnitTest {
System.out.println("Students before shuffling:"); System.out.println("Students before shuffling:");
System.out.println(studentsById.values()); System.out.println(studentsById.values());
List<Integer> shuffledStudentIds = new ArrayList<>(studentsById.keySet()); List<Map.Entry<Integer, String>> shuffledStudentEntries = new ArrayList<>(studentsById.entrySet());
Collections.shuffle(shuffledStudentIds); Collections.shuffle(shuffledStudentEntries);
List<String> shuffledStudents = shuffledStudentIds.stream() List<String> shuffledStudents = shuffledStudentEntries.stream()
.map(id -> studentsById.get(id)) .map(Map.Entry::getValue)
.collect(Collectors.toList()); .collect(Collectors.toList());
System.out.println("Students after shuffling"); System.out.println("Students after shuffling");
System.out.println(shuffledStudents); System.out.println(shuffledStudents);