title: java find the smallest of two numbers coming from two different object types when one can be null
body: <p>I'm trying to figure out the best way of grabbing the smallest of two numbers when the numbers are attributes inside of two different objects.  Each, but not both, of the objects can be null, which can lead to null pointer exceptions.  Each object has their own getValue() method, which will return a Long value.  There's the basic if/else that I'd prefer not to do:</p>
  
  <pre><code>if (obj1 != null &amp;&amp; obj2 != null) {  // neither object is null
      minValue = obj1.getValue() &lt;= obj2.getValue() ? obj1.getValue() : obj2.getValue();
  } else if (obj1 == null &amp;&amp; obj2 != null) { // only obj1 is null
      minValue = obj2.getValue();
  } else { // only obj2 is null (they can't both be null, so we don't need an if for the situation where they're both null)
      minValue = obj1.getValue();
  }
  </code></pre>
  
  <p>I've tried some other things:</p>
  
  <pre><code>// can throw null pointer exception
  Collections.min(Arrays.asList(obj1.getValue(), obj2.getValue()));
  
  // while both objects have a getValue() method, they are of different types, so mapping doesn't work
  Collections.min(Arrays.asList(obj1, obj2)
      .filter(obj -&gt; obj != null)
      .map(obj -&gt; obj.getValue()) // this line will fail since the methods correspond to different objects
      .collect(Collectors.toList()));
  </code></pre>
  
  <p>I feel like this should be a fairly easy problem, but my brain's not allowing it to work.  There's due to be some min function where you can bipass a situation where the object can be null?</p>