Skip to content

Instantly share code, notes, and snippets.

@poetix
Last active December 6, 2017 17:08
Show Gist options
  • Select an option

  • Save poetix/828654fae68955c4d04652c064c4df34 to your computer and use it in GitHub Desktop.

Select an option

Save poetix/828654fae68955c4d04652c064c4df34 to your computer and use it in GitHub Desktop.

Revisions

  1. poetix revised this gist Dec 6, 2017. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion MapValueRetrievalExample.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    public String ofNullableExample(Map<String, String> map) {
    return Optional.ofNullable(map.get(key)).orElseThrow(() -> new ItemNotFoundException(key));
    return Optional.ofNullable(map.get(key))
    .orElseThrow(() -> new ItemNotFoundException(key));
    }

    public String nullCheckingExample(Map<String, String> map) {
  2. poetix created this gist Dec 6, 2017.
    11 changes: 11 additions & 0 deletions MapValueRetrievalExample.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    public String ofNullableExample(Map<String, String> map) {
    return Optional.ofNullable(map.get(key)).orElseThrow(() -> new ItemNotFoundException(key));
    }

    public String nullCheckingExample(Map<String, String> map) {
    String valueAtKey = map.get(key);
    if (valueAtKey == null) {
    throw new ItemNotFoundException(key);
    }
    return valueAtKey;
    }