BAEL-2514 - Added a new test for retrieving values based on key patterns

This commit is contained in:
Juan Vaccari 2019-02-02 17:19:28 +00:00
parent 04626e522a
commit d56efcfa97
1 changed files with 17 additions and 0 deletions

View File

@ -3,11 +3,14 @@ package com.baeldung.stream;
import org.junit.Before;
import org.junit.Test;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@ -88,4 +91,18 @@ public class StreamMapUnitTest {
assertTrue(isbnCodes.isEmpty());
}
@Test
public void ValuesFromMapBasedOnPattern() {
Map<String, Integer> authorToYearOfBirth = new HashMap<>();
authorToYearOfBirth.put("Asimov, Isaac", 1920);
authorToYearOfBirth.put("Adams, Douglas", 1952);
authorToYearOfBirth.put("Bradbury, Ray", 1920);
authorToYearOfBirth.put("Clarke, Arthur", 1917);
List<Integer> yearOfBirthAuthorsStartingWithA = authorToYearOfBirth.entrySet().stream().filter(e -> e.getKey().startsWith("A")).map(Map.Entry::getValue).collect(Collectors.toList());
assertEquals(2, yearOfBirthAuthorsStartingWithA.size());
}
}