On Github tginsberg / java-optional
Created by Todd Ginsberg for CJUG 2016-06-02
new ArrayList<String>().stream().findFirst();
public interface Map<K,V> { // ... default Optional<V> getOptionally(K k) { // ... } }
Map<String, String> map = new HashMap<>(); map.put("A",null); // Both look the same - no difference between not present and null. map.getOptionally("A"); map.getOptionally("B");
Optional<String> text = Optional.ofNullable("Hello, world!");
Optional<String> text = Optional.of(somethingThatIsntNull);
Optional<String> text = Optional.of(null); // -> NullPointerException
// Yes public Optional<Customer> getCustomerById(long id);
// No public Optional<List<Customer>> getCustomersForMonth(final YearMonth when);
// Prefer: public List<Customer> getCustomersForMonth(final YearMonth when); // -> Collections.emptyList();
Optional<String> text = service.getText(); if(text.isPresent()) { System.out.println(text.get()); } else { System.out.println("Hello, world!"); }
String text = service.getText(); if(text != null) { System.out.println(text); } else { System.out.println("Hello, world!"); }
Optional<String> text = service.getText(); System.out.println(text.orElse("Hello, world!"));
Optional<String> text = service.getText(); text.ifPresent(this::doSomethingWithText);
Optional<String> text = service.getText(); System.out.println(text.orElseThrow(NoSuchElementException::new));