Java tutorial
//package com.java2s; import java.util.List; import java.util.Optional; public class Main { /** * @return the last element in the list, or empty if the list is empty */ public static <E> Optional<E> last(List<E> list) { if (list.isEmpty()) return Optional.empty(); return Optional.of(list.get(list.size() - 1)); } }