How to check if list is empty in Java 8 using Optional, if not null iterate through the list and print the object?

This checks whether a list is empty (or null) using Java 8 Optional, and if not, iterates through it and prints each element. Optional.ofNullable() safely wraps a possibly-null list, and orElseGet(Collections::emptyList) substitutes an empty list if it was null, avoiding a NullPointerException.

Code Example:

Optional.ofNullable(noteLst)
        .orElseGet(Collections::emptyList)
        .stream()
        .filter(Objects::nonNull)
        .map(Notes::getTagName)
        .forEach(System.out::println);