Skip to content

Instantly share code, notes, and snippets.

View mkp05's full-sized avatar
☠️

Tadeusz mkp05

☠️
View GitHub Profile
@Allan-Gong
Allan-Gong / flatmap_optionals.java
Created January 18, 2018 05:01
Options to flatMap a list of Optionals in Java8
// Ways to transform List<Optional<T>> to List<T> (excluding empty ones)
// 1. Using filter()
List<String> filteredList = listOfOptionals.stream()
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());
// 2. Using flatMap()
List<String> filteredList = listOfOptionals.stream()