title: Java 8 Collector that returns a value if there's only a single value
body: <p>I'm a little green on this functional programming and streams stuff, but what little I do know has been very useful!</p>
  
  <p>I've had this situation come up several times:</p>
  
  <pre><code>List&lt;SomeProperty&gt; distinctProperties = someList.stream()
      .map(obj -&gt; obj.getSomeProperty())
      .distinct()
      .collect(Collectors.toList());
  
  if (distinctProperties.size() == 1) {
      SomeProperty commonProperty = distinctProperties.get(0);
      // take some action knowing that all share this common property
  }
  </code></pre>
  
  <p>What I really want is:</p>
  
  <pre><code>Optional&lt;SomeProperty&gt; universalCommonProperty = someList.stream()
      .map(obj -&gt; obj.getSomeProperty())
      .distinct()
      .collect(Collectors.singleOrEmpty());
  </code></pre>
  
  <p>I think the <code>singleOrEmpty</code> thing can be useful in other situations besides just in combination with <code>distinct</code>.  When I was an uber n00b I spent a lot of time reinventing the Java Collections Framework because I didn't know it was there, so I'm trying not to repeat my mistakes.  Does Java come with a good way to do this <code>singleOrEmpty</code> thing?  Am I formulating it wrong?</p>
  
  <p>Thanks!</p>
  
  <p>EDIT: Here's some example data for the <code>distinct</code> case.  If you ignore the <code>map</code> step:</p>
  
  <pre><code>Optional&lt;SomeProperty&gt; universalCommonProperty = someList.stream()
      .map(obj -&gt; obj.getSomeProperty())
      .distinct()
      .collect(Collectors.singleOrEmpty());
  
  []     -&gt; Optional.empty()
  [1]    -&gt; Optional.of(1)
  [1, 1] -&gt; Optional.of(1)
  [2, 2] -&gt; Optional.of(2)
  [1, 2] -&gt; Optional.empty()
  </code></pre>
  
  <p>I find I need this when I screw up my types, or have legacy code.  It's really nice to be able to quickly say "All the elements of this collection share this property, so now I can take some action using this shared property."  Another example is when a user multi-selects some diverse elements, and you're trying to see what stuff you can do (if anything) that's valid for all of them.</p>
  
  <p>EDIT2: Sorry if my example is a misleading.  The key is <strong>singleOrEmpty</strong>.  I commonly find that I put a <code>distinct</code> in front, but it could just as easily be a <code>filter</code> of some other kind.</p>
  
  <pre><code>Optional&lt;SomeProperty&gt; loneSpecialItem = someList.stream()
      .filter(obj -&gt; obj.isSpecial())
      .collect(Collectors.singleOrEmpty());
  
  [special]           -&gt; Optional.of(special)
  [special, special]  -&gt; Optional.empty()
  [not]               -&gt; Optional.empty()
  [not, special]      -&gt; Optional.of(special)
  [not, special, not] -&gt; Optional.of(special)
  </code></pre>
  
  <p>EDIT3: I think I screwed up by motivating the <strong>singleOrEmpty</strong> instead of just asking for it on its own.</p>
  
  <pre><code>Optional&lt;Int&gt; value = someList.stream().collect(Collectors.singleOrEmpty())
  []     -&gt; Optional.empty()
  [1]    -&gt; Optional.of(1)
  [1, 1] -&gt; Optional.empty()
  </code></pre>