Skip to content

Instantly share code, notes, and snippets.

View knasim's full-sized avatar

Khurrum Nasim knasim

  • let the machine write it
  • washington dc
View GitHub Profile

Java 8: java.util.Optional

private abstract Optional<String> methodThatReturnsOptionalString();

private void useOptionalString() {
	Optional<String> myOptionalString = methodThatReturnsOptionalString();
	if (myOptionalString.isPresent()) {
		String myUnboxedString = myOptionalString.get();
		System.out.format("Optional string had value: %s\n", myUnboxedString);

} else {