Skip to content

Instantly share code, notes, and snippets.

@MikeChristianson
Last active September 12, 2016 05:15
Show Gist options
  • Save MikeChristianson/e999b37f92edc3cc52b05117b4e6f160 to your computer and use it in GitHub Desktop.
Save MikeChristianson/e999b37f92edc3cc52b05117b4e6f160 to your computer and use it in GitHub Desktop.
Get an Optional first element of a List.
/**
* Safely gets the first element of a {@link List}
* @param list a {@code List}
* @param <E> type of elements in list
* @return the first element of list or {@link Optional#absent()} if absent or null
*/
@Nonnull
private <E> Optional<E> first(@Nullable List<E> list) {
if (list == null || list.isEmpty()) {
return Optional.absent();
}
return Optional.fromNullable(list.get(0));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment