From add090f2e1a4ccb73e9a434d985432cb94b30a9c Mon Sep 17 00:00:00 2001 From: sanitaso <98709855+sanitaso@users.noreply.github.com> Date: Tue, 12 Apr 2022 05:15:51 +0200 Subject: [PATCH] BAEL-5416 How to Store HashMap Inside a List (#12047) * add unit test * fix the error * correcting the name of the test * trigger build process --- .../ListOfHashMapsUnitTest.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 core-java-modules/core-java-collections-list/src/test/java/com/baeldung/list/listOfHashMaps/ListOfHashMapsUnitTest.java diff --git a/core-java-modules/core-java-collections-list/src/test/java/com/baeldung/list/listOfHashMaps/ListOfHashMapsUnitTest.java b/core-java-modules/core-java-collections-list/src/test/java/com/baeldung/list/listOfHashMaps/ListOfHashMapsUnitTest.java new file mode 100644 index 0000000000..39110aedd3 --- /dev/null +++ b/core-java-modules/core-java-collections-list/src/test/java/com/baeldung/list/listOfHashMaps/ListOfHashMapsUnitTest.java @@ -0,0 +1,36 @@ +package test.java.com.baeldung.list.listOfHashMaps; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.stream.Collectors; + +import static org.junit.Assert.assertTrue; + +public class ListOfHashMapsUnitTest { + List>> booksAuthorsMapsList = new ArrayList<>(); + + @Test + public void givenMaps_whenAddToList_thenListContainsMaps() { + HashMap> javaBooksAuthorsMap = new HashMap<>(); + HashMap> phpBooksAuthorsMap = new HashMap<>(); + javaBooksAuthorsMap.put("Head First Java", Arrays.asList("Kathy Sierra", "Bert Bates")); + javaBooksAuthorsMap.put("Effective Java", Arrays.asList("Joshua Bloch")); + javaBooksAuthorsMap.put("OCA Java SE 8", + Arrays.asList("Kathy Sierra", "Bert Bates", "Elisabeth Robson")); + phpBooksAuthorsMap.put("The Joy of PHP", Arrays.asList("Alan Forbes")); + phpBooksAuthorsMap.put("Head First PHP & MySQL", + Arrays.asList("Lynn Beighley", "Michael Morrison")); + + booksAuthorsMapsList.add(javaBooksAuthorsMap); + booksAuthorsMapsList.add(phpBooksAuthorsMap); + + assertTrue(booksAuthorsMapsList.get(0).keySet().containsAll + (javaBooksAuthorsMap.keySet().stream().collect(Collectors.toList()))); + assertTrue(booksAuthorsMapsList.get(1).keySet().containsAll + (phpBooksAuthorsMap.keySet().stream().collect(Collectors.toList()))); + } +}