title: java 8 how to get distinct list on more than one property
body: <p>How can one get the distinct (distinct based on two property) list from a list of objects.
  for example let there are list of objects with property name and price.
  Now how can I get a list with distinct name or price.<br>
  suppose   </p>
  
  <pre><code>list&lt;xyz&gt; l1 = getlist(); // getlist will return the list.
  </code></pre>
  
  <p>Now let l1 has the following properties(name, price) :-<br>
  n1, p1<br>
  n1, p2<br>
  n2, p1<br>
  n2, p3  </p>
  
  <p>Now after the filter the list should be-<br>
  n1, p1<br>
  n2, p3</p>
  
  <p>I tried solving like this -</p>
  
  <pre><code>public List&lt;xyz&gt; getFilteredList(List&lt;xyz&gt; l1) {
  
          return l1
                  .stream()
                  .filter(distinctByKey(xyz::getName))
                  .filter(distinctByKey(xyz::getPrice))
                  .collect(Collectors.toList());
      }
  
      private 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;
      }
  </code></pre>
  
  <p>Now the problem is when i did filter on name the list return would be -<br>
  n1, p1<br>
  n2, p1</p>
  
  <p>and then it would have run filter on price which return -<br>
  n1, p1</p>
  
  <p>which is not the expected result.</p>