{"id":79797,"date":"2025-05-27T13:05:33","date_gmt":"2025-05-27T07:35:33","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=79797"},"modified":"2025-08-04T16:00:54","modified_gmt":"2025-08-04T10:30:54","slug":"mastering-java-streams-api","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/mastering-java-streams-api\/","title":{"rendered":"Mastering Java Streams API: A Beginner&#8217;s Guide with Practical Examples"},"content":{"rendered":"\n<p>Ever feel bogged down with endless loops and wordy code as you go about handling data in Java? Worry no more, as the Java Streams API comes in the least expected way-with simplicity and power combined. Its functional programming approach makes you look at processing data in a whole new way. We&#8217;ll dig into the magic of Java Streams with practical examples: making your code not just efficient but elegant. This is a guide for beginners. Yes! Code smarter, not harder. We&#8217;re all set to master the flow! Let&#8217;s get started!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is a Stream?<\/strong><\/h2>\n\n\n\n<p>A Stream in <a href=\"https:\/\/www.guvi.in\/courses\/programming\/java-programming\/?utm_source=blog&amp;utm_medium=organic&amp;utm_campaign=mastering_java_streams_api\" target=\"_blank\" rel=\"noreferrer noopener\">Java<\/a>, as part of the Java Streams API, represents a sequence of elements supporting various operations to process these elements. It\u2019s important to note that streams do not store data\u2014they only provide a view on the underlying data source (like arrays, collections, or I\/O channels).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. Creating Streams<\/strong><\/h3>\n\n\n\n<p>You can create a stream in various ways:<\/p>\n\n\n\n<ul>\n<li>Using Stream.of()<\/li>\n\n\n\n<li>From Collections using stream() or parallelStream()<\/li>\n\n\n\n<li>Using Arrays.stream()<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>import java.util.*;<br>import java.util.stream.*;<br><br>public class Main{<br>&nbsp; &nbsp; public static void main(String[] args) {<br>&nbsp; &nbsp; &nbsp; &nbsp; \/\/ Creating a Stream from a Collection<br>&nbsp; &nbsp; &nbsp; &nbsp; List&lt;String&gt; names = Arrays.asList(&#8220;John&#8221;, &#8220;Jane&#8221;, &#8220;Jack&#8221;, &#8220;Doe&#8221;);<br>&nbsp; &nbsp; &nbsp; &nbsp; Stream&lt;String&gt; nameStream = names.stream();<br><br>&nbsp; &nbsp; &nbsp; &nbsp; \/\/ Creating a Stream from an Array<br>&nbsp; &nbsp; &nbsp; &nbsp; String[] array = {&#8220;One&#8221;, &#8220;Two&#8221;, &#8220;Three&#8221;};<br>&nbsp; &nbsp; &nbsp; &nbsp; Stream&lt;String&gt; arrayStream = Arrays.stream(array);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; \/\/ Creating a Stream using Stream.of()<br>&nbsp; &nbsp; &nbsp; &nbsp; Stream&lt;Integer&gt; numberStream = Stream.of(1, 2, 3, 4, 5);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; nameStream.forEach(System.out::println);<br>&nbsp; &nbsp; }<br>}<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Stream Operations<\/strong><\/h3>\n\n\n\n<p>Stream operations are divided into <strong>intermediate<\/strong> and <strong>terminal<\/strong> operations:<\/p>\n\n\n\n<ul>\n<li><strong>Intermediate Operations<\/strong>: Return a new stream (e.g., filter(), map(), sorted()).<\/li>\n\n\n\n<li><strong>Terminal Operations<\/strong>: Produce a result or side effect (e.g., forEach(), collect(), count()).<\/li>\n<\/ul>\n\n\n\n<p><strong>Explore: <a href=\"https:\/\/www.guvi.in\/blog\/java-21-virtual-threads\/\" target=\"_blank\" rel=\"noreferrer noopener\">Java 21 Virtual Threads<\/a><\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Common Stream Methods with Examples<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>3.1 filter()<\/strong><\/h4>\n\n\n\n<p>The filter() method filters elements based on a given predicate (condition).<\/p>\n\n\n\n<p>java<\/p>\n\n\n\n<p>Copy code<\/p>\n\n\n\n<p>List&lt;Integer&gt; numbers = Arrays.asList(1, 2, 3, 4, 5, 6);<\/p>\n\n\n\n<p>List&lt;Integer&gt; evenNumbers = numbers.stream()<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.filter(num -&gt; num % 2 == 0)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.collect(Collectors.toList());<\/p>\n\n\n\n<p>System.out.println(&#8220;Even Numbers: &#8221; + evenNumbers);<\/p>\n\n\n\n<p><strong>Output:<\/strong><strong><br><\/strong>Even Numbers: [2, 4, 6]<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>3.2 map()<\/strong><\/h4>\n\n\n\n<p>The map() method transforms each element in a stream into another form using a function.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>List&lt;String&gt; names = Arrays.asList(&#8220;john&#8221;, &#8220;doe&#8221;, &#8220;mary&#8221;);<br>List&lt;String&gt; capitalizedNames = names.stream()<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(String::toUpperCase)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());<br>System.out.println(&#8220;Capitalized Names: &#8221; + capitalizedNames);<br><br><strong>Output:<\/strong><br>Capitalized Names: [JOHN, DOE, MARY]<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>3.3 sorted()<\/strong><\/h4>\n\n\n\n<p>The sorted() method sorts elements in natural order or using a custom comparator.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>List&lt;String&gt; unsortedNames = Arrays.asList(&#8220;John&#8221;, &#8220;Jane&#8221;, &#8220;Alex&#8221;, &#8220;Chris&#8221;);<br>List&lt;String&gt; sortedNames = unsortedNames.stream()<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .sorted()<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());<br>System.out.println(&#8220;Sorted Names: &#8221; + sortedNames);<br><br><strong>Output:<\/strong><br>Sorted Names: [Alex, Chris, Jane, John]<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>3.4 distinct()<\/strong><\/h4>\n\n\n\n<p>The distinct() method removes duplicate elements in a stream.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>List&lt;Integer&gt; numbersWithDuplicates = Arrays.asList(1, 2, 2, 3, 4, 4, 5);<br>List&lt;Integer&gt; distinctNumbers = numbersWithDuplicates.stream()<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .distinct()<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());<br>System.out.println(&#8220;Distinct Numbers: &#8221; + distinctNumbers);<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Output:<br>Distinct Numbers: [1, 2, 3, 4, 5]<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>3.5 limit() and skip()<\/strong><\/h4>\n\n\n\n<ul>\n<li>limit() restricts the number of elements in the stream.<\/li>\n\n\n\n<li>skip() skips the specified number of elements.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>List&lt;Integer&gt; numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);<br>List&lt;Integer&gt; limitedNumbers = numbers.stream().limit(5).collect(Collectors.toList());<br>List&lt;Integer&gt; skippedNumbers = numbers.stream().skip(5).collect(Collectors.toList());<br><br>System.out.println(&#8220;Limited Numbers: &#8221; + limitedNumbers);&nbsp;\/\/ Output: [1, 2, 3, 4, 5]<br>System.out.println(&#8220;Skipped Numbers: &#8221; + skippedNumbers);&nbsp;\/\/ Output: [6, 7, 8, 9]<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>3.6 collect()<\/strong><\/h4>\n\n\n\n<p>The collect() method collects the elements of a stream into a collection, such as a List, Set, or Map.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>List&lt;String&gt; names = Arrays.asList(&#8220;John&#8221;, &#8220;Jane&#8221;, &#8220;Jack&#8221;);<br>Set&lt;String&gt; nameSet = names.stream().collect(Collectors.toSet());<br>System.out.println(&#8220;Set of Names: &#8221; + nameSet);<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>3.7 reduce()<\/strong><\/h4>\n\n\n\n<p>The reduce() method performs a reduction on the elements of the stream using an associative accumulator function and returns an Optional value.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>List&lt;Integer&gt; numbers = Arrays.asList(1, 2, 3, 4, 5);<br>int sum = numbers.stream().reduce(0, (a, b) -&gt; a + b);<br>System.out.println(&#8220;Sum of Numbers: &#8221; + sum);<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>3.8 forEach()<\/strong><\/h4>\n\n\n\n<p>The forEach() method iterates over each element in the stream and performs a given action.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Stream&lt;String&gt; nameStream = Stream.of(&#8220;Alice&#8221;, &#8220;Bob&#8221;, &#8220;Charlie&#8221;);<br>nameStream.forEach(name -&gt; System.out.println(&#8220;Hello, &#8221; + name));<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. Parallel Streams<\/strong><\/h3>\n\n\n\n<p>Streams can be converted to <a href=\"https:\/\/docs.oracle.com\/search\/?q=parallel+streams&amp;lang=en&amp;category=java&amp;product=en%2Fjava\" target=\"_blank\" data-type=\"link\" data-id=\"https:\/\/docs.oracle.com\/search\/?q=parallel+streams&amp;lang=en&amp;category=java&amp;product=en%2Fjava\" rel=\"noreferrer noopener\">parallel streams<\/a> for multi-threaded processing. It\u2019s useful for large collections to take advantage of multi-core processors.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>List&lt;Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);<br>int parallelSum = numbers.parallelStream().reduce(0, Integer::sum);<br>System.out.println(&#8220;Sum using Parallel Stream: &#8221; + parallelSum);<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Discover: <a href=\"https:\/\/www.guvi.in\/blog\/java-programs-for-freshers\/\" target=\"_blank\" rel=\"noreferrer noopener\">Top 10 Java Programs For Freshers<\/a><\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. Real-World Example: Using Streams to Process Data<\/strong><\/h3>\n\n\n\n<p>Let\u2019s see how to use multiple stream methods together for a more complex example:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>class Product {<br>&nbsp; &nbsp; String name;<br>&nbsp; &nbsp; int price;<br>&nbsp; &nbsp; Product(String name, int price) {<br>&nbsp; &nbsp; &nbsp; &nbsp; this.name = name;<br>&nbsp; &nbsp; &nbsp; &nbsp; this.price = price;<br>&nbsp; &nbsp; }<br>}<br><br>List&lt;Product&gt; products = Arrays.asList(<br>&nbsp; &nbsp; new Product(&#8220;Laptop&#8221;, 800),<br>&nbsp; &nbsp; new Product(&#8220;Phone&#8221;, 600),<br>&nbsp; &nbsp; new Product(&#8220;Tablet&#8221;, 400),<br>&nbsp; &nbsp; new Product(&#8220;TV&#8221;, 900)<br>);<br><br>List&lt;String&gt; affordableProducts = products.stream()<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(p -&gt; p.price &lt; 700)&nbsp; \/\/ Select products below $700<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(p -&gt; p.name)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/ Get the names of these products<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .sorted()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/ Sort the names<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());<br><br>System.out.println(&#8220;Affordable Products: &#8221; + affordableProducts);<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Output:<br><\/strong>Affordable Products: [Phone, Tablet]<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>Congratulations! Just unlocked the potential of Java Streams API, a toolbox that turns simple data operations into art. From filtering and mapping to reducing and collecting, you are now equipped with skills to craft cleaner, more efficient <a href=\"https:\/\/www.guvi.in\/blog\/learn-java-programming-language\/\" target=\"_blank\" rel=\"noreferrer noopener\">Java code.<\/a> And the most exciting part: streams are just the first piece of what Java will offer today! Experiment further, keep learning, and let the stream of possibilities flow. Boost your <a href=\"https:\/\/www.guvi.in\/blog\/the-ultimate-java-developer-skills\/\" target=\"_blank\" rel=\"noreferrer noopener\">Java skills<\/a> by <a href=\"https:\/\/www.guvi.in\/ide\/\" target=\"_blank\" rel=\"noreferrer noopener\">practicing code directly in the GUVI IDE<\/a>\u2014hands-on learning made easy and effective!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ever feel bogged down with endless loops and wordy code as you go about handling data in Java? Worry no more, as the Java Streams API comes in the least expected way-with simplicity and power combined. Its functional programming approach makes you look at processing data in a whole new way. We&#8217;ll dig into the [&hellip;]<\/p>\n","protected":false},"author":40,"featured_media":80355,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[720],"tags":[],"views":"1586","authorinfo":{"name":"Lavish Jain","url":"https:\/\/www.guvi.in\/blog\/author\/lavish-jain\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/05\/Java-Streams-API-300x112.webp","jetpack_featured_media_url":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/05\/Java-Streams-API.webp","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/79797"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=79797"}],"version-history":[{"count":10,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/79797\/revisions"}],"predecessor-version":[{"id":84698,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/79797\/revisions\/84698"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/80355"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=79797"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=79797"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=79797"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}