Thursday, 25 July 2019

Converting an Optional to a List

Just a quick small snippet on how to convert a Optional to a List.

Hope it helps someone, who's new at Optionals.

public <T> List<T> convert(Optional<T> optT)
{
return optT.map(Collections::singletonList)
.orElse(Collections.emptyList());
}

No comments:

Post a Comment