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:
parent
b35b913cbe
commit
d6407ef405
|
@ -22,7 +22,7 @@ public class ShufflingCollectionsUnitTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void whenShufflingMapKeys_thenValuesAreShuffled() {
|
||||
public void whenShufflingMapEntries_thenValuesAreShuffled() {
|
||||
Map<Integer, String> 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<Integer> shuffledStudentIds = new ArrayList<>(studentsById.keySet());
|
||||
Collections.shuffle(shuffledStudentIds);
|
||||
List<Map.Entry<Integer, String>> shuffledStudentEntries = new ArrayList<>(studentsById.entrySet());
|
||||
Collections.shuffle(shuffledStudentEntries);
|
||||
|
||||
List<String> shuffledStudents = shuffledStudentIds.stream()
|
||||
.map(id -> studentsById.get(id))
|
||||
.collect(Collectors.toList());
|
||||
List<String> shuffledStudents = shuffledStudentEntries.stream()
|
||||
.map(Map.Entry::getValue)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
System.out.println("Students after shuffling");
|
||||
System.out.println(shuffledStudents);
|
||||
|
|
Loading…
Reference in New Issue