<?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; Collections</title>
	<atom:link href="https://www.lambdafaq.org/category/collections/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>What is a stream?</title>
		<link>https://www.lambdafaq.org/what-is-a-stream/</link>
		<comments>https://www.lambdafaq.org/what-is-a-stream/#comments</comments>
		<pubDate>Sat, 09 Mar 2013 13:34:07 +0000</pubDate>
		<dc:creator><![CDATA[naftalin]]></dc:creator>
				<category><![CDATA[Collections]]></category>

		<guid isPermaLink="false">http://www.lambdafaq.org/?p=904</guid>
		<description><![CDATA[<p>A stream is a sequence of values. The package java.util.stream defines types for streams of reference values (Stream) and some primitives (IntStream, LongStream, and DoubleStream). Streams are like iterators in that they yield their elements as required for processing, but unlike them in that they are not associated with any particular storage mechanism. A stream [...]]]></description>
				<content:encoded><![CDATA[<p>A stream is a sequence of values. The package <a href="http://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html"><code>java.util.stream</code></a> defines types for streams of reference values (<a href="http://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html"><code>Stream</code></a>) and some primitives (<a href="http://docs.oracle.com/javase/8/docs/api/java/util/stream/IntStream.html"><code>IntStream</code></a>, <a href="http://docs.oracle.com/javase/8/docs/api/java/util/stream/LongStream.html"><code>LongStream</code></a>, and <a href="http://docs.oracle.com/javase/8/docs/api/java/util/stream/DoubleStream.html"><code>DoubleStream</code></a>). Streams are like iterators in that they yield their elements as required for processing, but unlike them in that they are not associated with any particular storage mechanism. A stream is either <em>partially evaluated</em>&mdash;some of its elements remain to be generated&mdash;or <em>exhausted</em>, when its elements are all used up. A stream can have as its source an array, a collection, a generator function, or an IO channel; alternatively, it may be the result of an operation on another stream (see below). A partially evaluated stream may have infinitely many elements still to be generated, for example by a generator function.</p>
<p>Stream types define <em>intermediate operations</em> (resulting in new streams), e.g. <code>map</code>, and <em>terminal operations</em> (resulting in non-stream values), e.g. <code>forEach</code>. Calls on intermediate operations are often chained together in the style of a fluent API, forming a pipeline (as <a title="Where is the Java Collections Framework going?" href="where-is-the-java-collections-framework-going/">previously described</a>). Terminal operations, as the name implies, terminate a method chain. Terminal operations are also called <em>eager</em> because invoking them causes them to consume values from the pipeline immediately, whereas intermediate operations, also called <em>lazy</em>, only produce values on demand. For example, assuming that <code>strings</code> has been declared as a <code>List&lt;String></code>, this code:</p>
<pre><code>     IntStream ints = strings.stream().mapToInt(s -> s.length()).filter(i -> i%2 != 0);
</code></pre>
<p>sets up a pipeline which will first produce a stream of <code>int</code> values corresponding to the lengths of the elements of <code>strings</code>, then pass on the odd ones only. But none of this happens as a result of the declaration of <code>ints</code>. Processing only takes place when a statement like </p>
<pre><code>     ints.forEach(System.out::println);
</code></pre>
<p>uses an eager terminal operation to pull values down the pipeline.</p>
<p>The following table shows a small sample of operations on <code>Stream</code>. These have been chosen for simplicity; also, in the same cause, bounded generic types in their signatures have been replaced by their bounds. (Intermediate and terminal stream operations are listed in greater detail here (tbs) and here (tbs).)</p>
<table>
<tr>
<th>operation</th>
<th>interface used</th>
<th>&lambda; signature</th>
<th>return type</th>
<th>return value</th>
</tr>
<tr>
<td colspan="4" style="text-align:center"><em>sample lazy/intermediate operations</em></td>
</tr>
<tr>
<td>filter</td>
<td><a href="http://docs.oracle.com/javase/8/docs/api/java/util/function/Predicate.html">Predicate&lt;T></a></td>
<td>T ➞ boolean</td>
<td>Stream&lt;T&gt;</td>
<td>stream containing <span class="glossary-term" onmouseover="TagToTip('Span2', FOLLOWMOUSE, false, STICKY, true, DURATION, 5000)">input elements</span> that satisfy the <code>Predicate</code></td>
</tr>
<tr>
<td>map</td>
<td><a href="http://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html">Function&lt;T,R></a></td>
<td>T ➞ R</td>
<td>Stream&lt;R></td>
<td>stream of values, the result of applying the  <code>Function</code> to each input element</tr>
<tr>
<td>sorted</td>
<td><a href="http://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html">Comparator&lt;T></a></td>
<td>(T, T) ➞ int </td>
<td>Stream&lt;T></td>
<td>stream containing the input elements, sorted by the <code>Comparator</code></tr>
<tr>
<td>limit, skip</td>
<td></td>
<td></td>
<td>Stream&lt;T></td>
<td>stream including only (resp. skipping) first <em>n</em> input elements</tr>
<tr>
<td colspan="4" style="text-align:center"><em>sample eager/terminal operations</em></td>
<td/></tr>
<tr>
<td>reduce</td>
<td><a href="http://docs.oracle.com/javase/8/docs/api/java/util/function/BinaryOperator.html">BinaryOperator&lt;T></a></td>
<td>(T, T) ➞ T</td>
<td>Optional&lt;T></td>
<td>result of reduction of input elements (if any) using supplied <code>BinaryOperator</code></td>
</tr>
<tr>
<td>findFirst</td>
<td</td>
<td><a href="http://docs.oracle.com/javase/8/docs/api/java/util/function/Predicate.html">Predicate&lt;T></a></td>
<td>T ➞ boolean</td>
<td>Optional&lt;T></td>
<td>first input element satisfying <code>Predicate</code> (if any)</td>
</tr>
<tr>
<td>forEach</td>
<td><a href="http://docs.oracle.com/javase/8/docs/api/java/util/function/Consumer.html">Consumer&lt;T></a></td>
<td>T ➞ void</td>
<td>void</td>
<td>void, but applies the method of supplied <code>Consumer</code> to every input element</tr>
</table>
<p>Streams may be <em>ordered</em> or <em>unordered</em>.  A stream whose source is an array, a <code>List</code>, or a generator function, is ordered; one whose source is a <code>Set</code> is unordered. Order is preserved by most intermediate operations; exceptions are <code>sorted</code>, which <em>imposes</em> an ordering whether one was previously present or not, and <code>unordered</code>, which <em>removes</em> any ordering that was present on the receiver. (This operation is provided for situations where ordering is not significant for the terminal operation, but the developer wants to take advantage of the greater efficiency of some operations when executed in parallel on unordered stream than on ordered ones.) Most terminal operations respect ordering; for example <code>toArray</code>, called on an ordered stream, creates an array with element ordering corresponding to that of the stream. An exception is <code>forEach</code>; the order in which stream elements are processed by this operation is undefined. </p>
<p><span id="Span2">That is, elements of the input stream.</span></p>
<p><!--
A characteristic pattern of collection handling in Java, pre-JDK 8, is the processing of a collection in a series of loops, which progressively transform or selectively transfer the contents of the collection to successive created collections, finally summarising the last one in some way, for example by reducing its contents or finding an extreme element.
--> </p>
]]></content:encoded>
			<wfw:commentRss>https://www.lambdafaq.org/what-is-a-stream/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
