「Java8」ListをMapへ変換するサンプルコード
Javaコード:
@Test
public void convert_list_to_map_with_java8_lambda () {
List<Movie> movies = new ArrayList<Movie>();
movies.add(new Movie(1, “ドクターX〜外科医"));
movies.add(new Movie(2, “ヒーロー"));
Map<Integer, Movie> mappedMovies = movies.stream().collect(
Collectors.toMap(Movie::getRank, (p) -> p));
logger.info(mappedMovies);
assertTrue(mappedMovies.size() == 2);
assertEquals(“ドクターX〜外科医", mappedMovies.get(1).getDescription());
}