<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Maurice Naftalin&#039;s Lambda FAQ &#187; Advanced Questions</title>
	<atom:link href="https://www.lambdafaq.org/category/advanced-questions/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.lambdafaq.org</link>
	<description>Your questions answered: all about Lambdas and friends</description>
	<lastBuildDate>Tue, 04 May 2021 15:50:56 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.38</generator>
	<item>
		<title>How can I get distinct() to compare some derived value instead of the stream elements themselves?</title>
		<link>https://www.lambdafaq.org/how-can-i-get-distinct-to-compare-some-derived-value-instead-of-the-stream-elements-themselves/</link>
		<comments>https://www.lambdafaq.org/how-can-i-get-distinct-to-compare-some-derived-value-instead-of-the-stream-elements-themselves/#comments</comments>
		<pubDate>Sat, 10 Jan 2015 07:07:03 +0000</pubDate>
		<dc:creator><![CDATA[smarks]]></dc:creator>
				<category><![CDATA[Advanced Questions]]></category>

		<guid isPermaLink="false">http://www.lambdafaq.org/?p=1238</guid>
		<description><![CDATA[<p>The distinct() stream operation compares the stream&#8217;s elements directly to each other using Object.equals(), but there&#8217;s no obvious way to have it compute uniqueness based on some value derived from each stream element.</p> <p>Here&#8217;s a snippet of code do this. This is used in a filter() operation, and it takes a key extractor function to [...]]]></description>
				<content:encoded><![CDATA[<p>The <code>distinct()</code> stream operation compares the stream&#8217;s elements directly to each other using <code>Object.equals()</code>, but there&#8217;s no obvious way to have it compute uniqueness based on some value derived from each stream element.</p>
<p>Here&#8217;s a snippet of code do this. This is used in a filter() operation, and it takes a <em>key extractor</em> function to derive a value from the stream element. This derived value is in turn used do determine uniqueness.</p>
<pre>public static &lt;T&gt; Predicate&lt;T&gt; distinctByKey(Function&lt;? super T,Object&gt; keyExtractor) {
    Map&lt;Object,String&gt; seen = new ConcurrentHashMap&lt;&gt;();
    return t -&gt; seen.put(keyExtractor.apply(t), "") == null;
}</pre>
<p>Here&#8217;s an example of its use. Given a collection of persons, this filters the list so that at most one person of any given age is output:</p>
<pre>persons.stream()
    .filter(distinctByKey(Person::getAge))
    .forEach(System.out::println);</pre>
<p>If the stream is sequential, the output will contain the first stream element. If the stream is parallel, the output will contain <em>one</em> element for each unique derived value, but it won&#8217;t necessarily be the first.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<pre></pre>
]]></content:encoded>
			<wfw:commentRss>https://www.lambdafaq.org/how-can-i-get-distinct-to-compare-some-derived-value-instead-of-the-stream-elements-themselves/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
