title: Java 8 map with Map.get nullPointer Optimization
body: <pre><code>public class StartObject{
       private Something something;
       private Set&lt;ObjectThatMatters&gt; objectThatMattersSet;
  }
  
  public class Something{
       private Set&lt;SomeObject&gt; someObjecSet;
  }
  
  public class SomeObject {
       private AnotherObject anotherObjectSet;
  }
  
  public class AnotherObject{
       private Set&lt;ObjectThatMatters&gt; objectThatMattersSet;
  }
  
  public class ObjectThatMatters{
       private Long id;
  }
  
  
  private void someMethod(StartObject startObject) {
      Map&lt;Long, ObjectThatMatters&gt; objectThatMattersMap = StartObject.getSomething()
              .getSomeObject.stream()
              .map(getSomeObject::getAnotherObject)
              .flatMap(anotherObject-&gt; anotherObject.getObjectThatMattersSet().stream())
              .collect(Collectors.toMap(ObjectThatMatters -&gt; ObjectThatMatters.getId(), Function.identity()));
     Set&lt;ObjectThatMatters &gt; dbObjectThatMatters = new HashSet&lt;&gt;();
     try {
          dbObjectThatMatters.addAll( tartObject.getObjectThatMatters().stream().map(objectThatMatters-&gt; objectThatMattersMap .get(objectThatMatters.getId())).collect(Collectors.toSet()));
      } catch (NullPointerException e) {
          throw new someCustomException();
      }
   startObject.setObjectThatMattersSet(dbObjectThatMatters);
  </code></pre>
  
  <p><strong>Given</strong> a StartObject that contains a set of ObjectThatMatters <br>
  <strong>And</strong> a Something that contains the database structure already fetched filled with all valid ObjectThatMatters.<br>
  <strong>When</strong> I want to swap the StartObject set of ObjectThatMatters to the valid corresponding db objects that only exist in the scope of the Something<br>
  <strong>Then</strong> I compare the set of ObjectThatMatters on the StartObject <br>
  <strong>And</strong> replace every one of them with the valid ObjectThatMatters inside the Something object <br>
  <strong>And</strong> If some ObjectThatMatters doesn't have a valid ObjectThatMatters I throw a someCustomException <br>
  <br>
  <strong>This someMethod seems pretty horrible, how can I make it more readable?</strong> <br>
  Already tried to change the try Catch to a optional but that doesn't actually help. <br>
  Used a Map instead of a List with List.contains because of performance, was this a good idea? The total number of ObjectThatMatters will be usually 500. <br>
  I'm not allowed to change the other classes structure and I'm only showing you the fields that affect this method not every field since they are extremely rich objects.</p>