Optional<String> (List<String> strings) { boolean seen = false; String best = null; for (String s : strings) { if (!s.isEmpty()) { if (!seen || s.compareTo(best) > 0) { seen = true; best = s; } } } return seen ? Optional.of(best) : Optional.empty(); }