public static < T > Optional < T > firstInList ( List < T > list ) { return list . isEmpty ( ) ? Optional . empty ( ) : Optional . ofNullable ( list . get ( 0 ) ) ; }
Adapter to create an Optional out of the first element of a List . If the list is empty we get an empty optional . Also if the first element is null return an empty optional .