title: Java Lambda Stream Distinct() on arbitrary key?
body: <p>I frequently ran into a problem with Java lambda expressions where when I wanted to distinct() a stream on an arbitrary property or method of an object, but wanted to keep the object rather than map it to that property or method. I started to create containers as discussed <a href="https://stackoverflow.com/questions/23699371/java-8-distinct-by-property">here</a> but I started to do it enough to where it became annoying and made a lot of boilerplate classes. </p>
  
  <p>I threw together this Pairing class, which holds two objects of two types and allows you to specify keying off the left, right, or both objects. My question is... is there really no built-in lambda stream function to distinct() on a key supplier of some sorts? That would really surprise me. If not, will this class fulfill that function reliably?</p>
  
  <p>Here is how it would be called</p>
  
  <pre><code>BigDecimal totalShare = orders.stream().map(c -&gt; Pairing.keyLeft(c.getCompany().getId(), c.getShare())).distinct().map(Pairing::getRightItem).reduce(BigDecimal.ZERO, (x,y) -&gt; x.add(y));
  </code></pre>
  
  <p>Here is the Pairing class</p>
  
  <pre><code>    public final class Pairing&lt;X,Y&gt;  {
             private final X item1;
             private final Y item2;
             private final KeySetup keySetup;
  
             private static enum KeySetup {LEFT,RIGHT,BOTH};
  
             private Pairing(X item1, Y item2, KeySetup keySetup) {
                    this.item1 = item1;
                    this.item2 = item2;
                    this.keySetup = keySetup;
             }
             public X getLeftItem() { 
                    return item1;
             }
             public Y getRightItem() { 
                    return item2;
             }
  
             public static &lt;X,Y&gt; Pairing&lt;X,Y&gt; keyLeft(X item1, Y item2) { 
                    return new Pairing&lt;X,Y&gt;(item1, item2, KeySetup.LEFT);
             }
  
             public static &lt;X,Y&gt; Pairing&lt;X,Y&gt; keyRight(X item1, Y item2) { 
                    return new Pairing&lt;X,Y&gt;(item1, item2, KeySetup.RIGHT);
             }
             public static &lt;X,Y&gt; Pairing&lt;X,Y&gt; keyBoth(X item1, Y item2) { 
                    return new Pairing&lt;X,Y&gt;(item1, item2, KeySetup.BOTH);
             }
             public static &lt;X,Y&gt; Pairing&lt;X,Y&gt; forItems(X item1, Y item2) { 
                    return keyBoth(item1, item2);
             }
  
             @Override
             public int hashCode() {
                    final int prime = 31;
                    int result = 1;
                    if (keySetup.equals(KeySetup.LEFT) || keySetup.equals(KeySetup.BOTH)) {
                    result = prime * result + ((item1 == null) ? 0 : item1.hashCode());
                    }
                    if (keySetup.equals(KeySetup.RIGHT) || keySetup.equals(KeySetup.BOTH)) {
                    result = prime * result + ((item2 == null) ? 0 : item2.hashCode());
                    }
                    return result;
             }
  
             @Override
             public boolean equals(Object obj) {
                    if (this == obj)
                           return true;
                    if (obj == null)
                           return false;
                    if (getClass() != obj.getClass())
                           return false;
                    Pairing&lt;?,?&gt; other = (Pairing&lt;?,?&gt;) obj;
                    if (keySetup.equals(KeySetup.LEFT) || keySetup.equals(KeySetup.BOTH)) {
                           if (item1 == null) {
                                 if (other.item1 != null)
                                        return false;
                           } else if (!item1.equals(other.item1))
                                 return false;
                    }
                    if (keySetup.equals(KeySetup.RIGHT) || keySetup.equals(KeySetup.BOTH)) {
                           if (item2 == null) {
                                 if (other.item2 != null)
                                        return false;
                           } else if (!item2.equals(other.item2))
                                 return false;
                    }
                    return true;
             }
  
      }
  </code></pre>
  
  <p>UPDATE:</p>
  
  <p>Tested Stuart's function below and it seems to work great. The operation below distincts on the first letter of each string. The only part I'm trying to figure out is how the ConcurrentHashMap maintains only one instance for the entire stream</p>
  
  <pre><code>public class DistinctByKey {
  
      public static &lt;T&gt; Predicate&lt;T&gt; distinctByKey(Function&lt;? super T,Object&gt; keyExtractor) {
          Map&lt;Object,Boolean&gt; seen = new ConcurrentHashMap&lt;&gt;();
          return t -&gt; seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
      }
  
      public static void main(String[] args) { 
  
          final ImmutableList&lt;String&gt; arpts = ImmutableList.of("ABQ","ALB","CHI","CUN","PHX","PUJ","BWI");
  
          arpts.stream().filter(distinctByKey(f -&gt; f.substring(0,1))).forEach(s -&gt; System.out.println(s));
      }
  </code></pre>
  
  <p>Output is...</p>
  
  <pre><code>ABQ
  CHI
  PHX
  BWI
  </code></pre>