<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Veerdevmca on Medium]]></title>
        <description><![CDATA[Stories by Veerdevmca on Medium]]></description>
        <link>https://medium.com/@veerdevmca?source=rss-77694c7e7c8a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*6rmlSK9SuY2i0Qtl</url>
            <title>Stories by Veerdevmca on Medium</title>
            <link>https://medium.com/@veerdevmca?source=rss-77694c7e7c8a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 13 Nov 2026 22:17:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@veerdevmca/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[Java 8 Stream API Explained with Practical Examples: A Complete Beginner-to-Intermediate Guide]]></title>
            <link>https://medium.com/@veerdevmca/java-8-stream-api-explained-with-practical-examples-a-complete-beginner-to-intermediate-guide-46321867e283?source=rss-77694c7e7c8a------2</link>
            <guid isPermaLink="false">https://medium.com/p/46321867e283</guid>
            <category><![CDATA[stream-api]]></category>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[java8]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Veerdevmca]]></dc:creator>
            <pubDate>Thu, 12 Nov 2026 07:20:28 GMT</pubDate>
            <atom:updated>2026-11-12T07:20:28.621Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Subtitle:</strong> Learn how to filter, map, sort, collect, and process Java collections using Stream API with practical examples and real-world scenarios.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-hmXH12JSy2f0buTqo-q1Q.png" /></figure><h3>Introduction</h3><p>Java is one of the most widely used programming languages in enterprise application development. Before Java 8, developers generally used loops and conditional statements to process collections such as Lists, Sets, and Maps.</p><p>Although traditional loops work perfectly fine, they can sometimes make code lengthy and difficult to maintain.</p><p>Java 8 introduced the <strong>Stream API</strong>, which provides a cleaner and more readable way to process collections using functional programming concepts.</p><p>In this article, we will understand the Java 8 Stream API from the basics and explore practical examples that can be used in real-world Java applications.</p><h3>1. What is Stream API in Java 8?</h3><p>The Stream API is a feature introduced in Java 8 that allows developers to process collections of data in a functional and declarative way.</p><p>It supports operations such as:</p><ul><li>Filtering data</li><li>Transforming data</li><li>Sorting elements</li><li>Finding matching elements</li><li>Removing duplicates</li><li>Grouping data</li><li>Calculating values</li></ul><p><strong>Important:</strong> A Stream does not store data. It processes data from a source such as a List, Set, or Array.</p><h3>Basic Stream workflow</h3><p>A Stream generally follows three steps:</p><ol><li>Source — The collection or data source.</li><li>Intermediate operations — Operations such as filter(), map(), and sorted().</li><li>Terminal operation — Operations such as collect(), forEach(), and count().</li></ol><p>Example workflow:</p><p>Collection → Stream → Intermediate Operations → Terminal Operation → Result</p><h3>2. Why do we use Stream API?</h3><p>Let’s understand the advantages.</p><ul><li><strong>Readable code:</strong> Reduces unnecessary loops and conditions.</li><li><strong>Less boilerplate:</strong> Makes collection processing simpler.</li><li><strong>Functional programming:</strong> Supports lambda expressions and method references.</li><li><strong>Parallel processing:</strong> Supports parallelStream() for certain workloads.</li><li><strong>Easy data transformation:</strong> Allows filtering, mapping, and sorting in a pipeline.</li></ul><h3>3. Creating a Stream</h3><p>There are different ways to create a Stream in Java 8.</p><h3>Example 1: Creating a Stream from a List</h3><pre>import java.util.Arrays;<br>import java.util.List;</pre><pre>public class StreamExample {</pre><pre>    public static void main(String[] args) {</pre><pre>        // Create a list of numbers<br>        List&lt;Integer&gt; numbers = Arrays.asList(10, 20, 30, 40, 50);</pre><pre>        // Convert the list into a stream<br>        numbers.stream()</pre><pre>               // Print each element<br>               .forEach(System.out::println);<br>    }<br>}</pre><p><strong>Output:</strong></p><pre>10<br>20<br>30<br>40<br>50</pre><p>Here, stream() converts the List into a Stream, and forEach() processes each element.</p><h3>4. Important Stream API Operations</h3><p>Let’s explore the most commonly used Stream operations.</p><h3>Example 2: filter()</h3><p>The filter() method is used to select elements based on a given condition.</p><p><strong>Scenario:</strong> Find all employees whose salary is greater than 50,000.</p><pre>import java.util.Arrays;<br>import java.util.List;</pre><pre>public class FilterExample {</pre><pre>    public static void main(String[] args) {</pre><pre>        // Create a list of employee salaries<br>        List&lt;Integer&gt; salaries = Arrays.asList(30000, 45000, 60000, 75000, 90000);</pre><pre>        // Filter salaries greater than 50000<br>        salaries.stream()<br>                .filter(salary -&gt; salary &gt; 50000)</pre><pre>                // Print filtered salaries<br>                .forEach(System.out::println);<br>    }<br>}</pre><p><strong>Output:</strong></p><pre>60000<br>75000<br>90000</pre><p><strong>Explanation:</strong> The filter() method checks every salary and keeps only the values greater than 50,000.</p><h3>Example 3: map()</h3><p>The map() method is used to transform each element into another value.</p><p><strong>Scenario:</strong> Increase every employee’s salary by 10%.</p><pre>import java.util.Arrays;<br>import java.util.List;</pre><pre>public class MapExample {</pre><pre>    public static void main(String[] args) {</pre><pre>        // Create a list of salaries<br>        List&lt;Integer&gt; salaries = Arrays.asList(30000, 40000, 50000);</pre><pre>        // Increase every salary by 10%<br>        salaries.stream()<br>                .map(salary -&gt; salary + (salary * 10 / 100))</pre><pre>                // Print updated salaries<br>                .forEach(System.out::println);<br>    }<br>}</pre><p><strong>Output:</strong></p><pre>33000<br>44000<br>55000</pre><p><strong>Explanation:</strong> The map() method transforms every salary by adding 10%.</p><h3>Example 4: sorted()</h3><p>The sorted() method is used to sort elements in ascending or descending order.</p><p><strong>Scenario:</strong> Sort employee salaries.</p><pre>import java.util.Arrays;<br>import java.util.List;<br>import java.util.Comparator;</pre><pre>public class SortedExample {</pre><pre>    public static void main(String[] args) {</pre><pre>        // Create a list of salaries<br>        List&lt;Integer&gt; salaries = Arrays.asList(50000, 30000, 70000, 40000);</pre><pre>        // Sort salaries in ascending order<br>        salaries.stream()<br>                .sorted()<br>                .forEach(System.out::println);</pre><pre>        System.out.println(&quot;Descending Order:&quot;);</pre><pre>        // Sort salaries in descending order<br>        salaries.stream()<br>                .sorted(Comparator.reverseOrder())<br>                .forEach(System.out::println);<br>    }<br>}</pre><p><strong>Output:</strong></p><pre>30000<br>40000<br>50000<br>70000</pre><pre>Descending Order:<br>70000<br>50000<br>40000<br>30000</pre><h3>Example 5: distinct()</h3><p>The distinct() method removes duplicate elements from a Stream.</p><p><strong>Scenario:</strong> Remove duplicate employee IDs.</p><pre>import java.util.Arrays;<br>import java.util.List;</pre><pre>public class DistinctExample {</pre><pre>    public static void main(String[] args) {</pre><pre>        // Create a list containing duplicate IDs<br>        List&lt;Integer&gt; ids = Arrays.asList(101, 102, 101, 103, 102);</pre><pre>        // Remove duplicate IDs<br>        ids.stream()<br>           .distinct()<br>           .forEach(System.out::println);<br>    }<br>}</pre><p><strong>Output:</strong></p><pre>101<br>102<br>103</pre><h3>Example 6: collect()</h3><p>The collect() method is used to collect processed Stream elements into a collection such as a List or Set.</p><p><strong>Scenario:</strong> Get a new List containing salaries greater than 40,000.</p><pre>import java.util.Arrays;<br>import java.util.List;<br>import java.util.stream.Collectors;</pre><pre>public class CollectExample {</pre><pre>    public static void main(String[] args) {</pre><pre>        // Create a list of salaries<br>        List&lt;Integer&gt; salaries = Arrays.asList(30000, 45000, 60000, 75000);</pre><pre>        // Filter and collect salaries into a new list<br>        List&lt;Integer&gt; result = salaries.stream()<br>                .filter(salary -&gt; salary &gt; 40000)<br>                .collect(Collectors.toList());</pre><pre>        // Print the resulting list<br>        System.out.println(result);<br>    }<br>}</pre><p><strong>Output:</strong></p><pre>[45000, 60000, 75000]</pre><h3>Example 7: count()</h3><p>The count() method returns the number of elements matching a condition.</p><pre>import java.util.Arrays;<br>import java.util.List;</pre><pre>public class CountExample {</pre><pre>    public static void main(String[] args) {</pre><pre>        // Create a list of numbers<br>        List&lt;Integer&gt; numbers = Arrays.asList(10, 20, 30, 40, 50);</pre><pre>        // Count numbers greater than 25<br>        long count = numbers.stream()<br>                .filter(number -&gt; number &gt; 25)<br>                .count();</pre><pre>        // Print the count<br>        System.out.println(&quot;Count: &quot; + count);<br>    }<br>}</pre><p><strong>Output:</strong></p><pre>Count: 3</pre><h3>5. Real-World Example: Employee Management</h3><p>Now let’s consider a practical scenario commonly found in enterprise applications.</p><p>Suppose we have an Employee class with the following properties:</p><ul><li>Employee ID</li><li>Employee Name</li><li>Department</li><li>Salary</li></ul><p>We need to find employees from the IT department whose salary is greater than 50,000.</p><h3>Employee.java</h3><pre>public class Employee {   <br> private int id;<br>    private String name;<br>    private String department;<br>    private double salary;</pre><pre>    // Constructor<br>    public Employee(int id, String name, String department, double salary) {<br>        this.id = id;<br>        this.name = name;<br>        this.department = department;<br>        this.salary = salary;<br>    }</pre><pre>    // Getter for employee name<br>    public String getName() {<br>        return name;<br>    }</pre><pre>    // Getter for department<br>    public String getDepartment() {<br>        return department;<br>    }</pre><pre>    // Getter for salary<br>    public double getSalary() {<br>        return salary;<br>    }<br>}</pre><h3>Main.java</h3><pre>import java.util.Arrays;<br>import java.util.List;</pre><pre>public class Main {</pre><pre>    public static void main(String[] args) {</pre><pre>        // Create employee records<br>        List&lt;Employee&gt; employees = Arrays.asList(<br>            new Employee(1, &quot;Akash&quot;, &quot;IT&quot;, 60000),<br>            new Employee(2, &quot;Rahul&quot;, &quot;HR&quot;, 40000),<br>            new Employee(3, &quot;Priya&quot;, &quot;IT&quot;, 75000),<br>            new Employee(4, &quot;Sneha&quot;, &quot;Finance&quot;, 55000),<br>            new Employee(5, &quot;Amit&quot;, &quot;IT&quot;, 45000)<br>        );</pre><pre>        // Find IT employees earning more than 50000<br>        employees.stream()<br>                .filter(emp -&gt; emp.getDepartment().equals(&quot;IT&quot;))<br>                .filter(emp -&gt; emp.getSalary() &gt; 50000)</pre><pre>                // Print employee details<br>                .forEach(emp -&gt;<br>                    System.out.println(<br>                        emp.getName() + &quot; - &quot; + emp.getSalary()<br>                    )<br>                );<br>    }<br>}</pre><p><strong>Output:</strong></p><pre>Akash - 60000.0<br>Priya - 75000.0</pre><p><strong>Real-world explanation:</strong></p><p>In enterprise applications, we frequently need to filter employee records based on department, salary, status, or location. Instead of writing multiple nested loops and conditions, we can use Stream API to perform these operations in a clean and readable way.</p><h3>6. Grouping Employees by Department</h3><p>One of the useful features of Stream API is groupingBy().</p><p><strong>Scenario:</strong> Group all employees according to their department.</p><pre>import java.util.*;<br>import java.util.stream.Collectors;</pre><pre>public class GroupExample {</pre><pre>    public static void main(String[] args) {</pre><pre>        // Create a list of employees<br>        List&lt;Employee&gt; employees = Arrays.asList(<br>            new Employee(1, &quot;Akash&quot;, &quot;IT&quot;, 60000),<br>            new Employee(2, &quot;Rahul&quot;, &quot;HR&quot;, 40000),<br>            new Employee(3, &quot;Priya&quot;, &quot;IT&quot;, 75000),<br>            new Employee(4, &quot;Sneha&quot;, &quot;Finance&quot;, 55000)<br>        );</pre><pre>        // Group employees by department<br>        Map&lt;String, List&lt;Employee&gt;&gt; result = employees.stream()<br>            .collect(Collectors.groupingBy(Employee::getDepartment));</pre><pre>        // Print department names and employee counts<br>        result.forEach((department, employeeList) -&gt;<br>            System.out.println(department + &quot; : &quot; + employeeList.size())<br>        );<br>    }<br>}</pre><p><strong>Output:</strong></p><pre>IT : 2<br>HR : 1<br>Finance : 1</pre><h3>7. Difference Between map() and flatMap()</h3><p>Both methods are used for data transformation, but they work differently.</p><p>map() flatMap() Transforms each element into one result Transforms each element into a Stream and flattens the results One-to-one mapping One-to-many mapping is possible Does not flatten nested collections Flattens nested collections</p><p><strong>Example using flatMap():</strong></p><pre>import java.util.*;<br>import java.util.stream.Collectors;</pre><pre>public class FlatMapExample {</pre><pre>    public static void main(String[] args) {</pre><pre>        // Create a list containing multiple lists<br>        List&lt;List&lt;String&gt;&gt; names = Arrays.asList(<br>            Arrays.asList(&quot;Akash&quot;, &quot;Rahul&quot;),<br>            Arrays.asList(&quot;Priya&quot;, &quot;Sneha&quot;)<br>        );</pre><pre>        // Flatten nested lists into a single stream<br>        List&lt;String&gt; result = names.stream()<br>                .flatMap(list -&gt; list.stream())<br>                .collect(Collectors.toList());</pre><pre>        // Print flattened list<br>        System.out.println(result);<br>    }<br>}</pre><p><strong>Output:</strong></p><pre>[Akash, Rahul, Priya, Sneha]</pre><h3>8. What is the difference between Intermediate and Terminal Operations?</h3><p>Intermediate Operations Terminal Operations Return another Stream Produce a final result or side effect Lazy in execution Trigger Stream execution Can be chained End the Stream pipeline Examples: filter(), map(), sorted()Examples: collect(), count(), forEach()</p><p><strong>Important concept: Lazy Evaluation</strong></p><p>Intermediate operations are not executed immediately. They are executed only when a terminal operation is invoked.</p><p>For example:</p><pre>numbers.stream()<br>       .filter(n -&gt; n &gt; 20)<br>       .map(n -&gt; n * 2)<br>       .collect(Collectors.toList());</pre><p>The filter() and map() operations are executed when collect() triggers the Stream pipeline.</p><h3>9. What is Parallel Stream?</h3><p>Java 8 also introduced parallelStream(), which allows collection elements to be processed using multiple threads through the ForkJoin common pool.</p><p>Example:</p><pre>import java.util.Arrays;<br>import java.util.List;</pre><pre>public class ParallelStreamExample {</pre><pre>    public static void main(String[] args) {</pre><pre>        // Create a list of numbers<br>        List&lt;Integer&gt; numbers = Arrays.asList(10, 20, 30, 40, 50);</pre><pre>        // Process elements using a parallel stream<br>        numbers.parallelStream()<br>               .forEach(number -&gt;<br>                   System.out.println(<br>                       number + &quot; : &quot; +   Thread.currentThread().getName()<br>                   )<br>               );<br>    }<br>}</pre><p><strong>Sample output:</strong></p><pre>30 : main<br>20 : ForkJoinPool.commonPool-worker-1<br>10 : ForkJoinPool.commonPool-worker-2<br>40 : main<br>50 : ForkJoinPool.commonPool-worker-1</pre><p>Note: The output order and thread names can vary between executions.</p><p><strong>When should we use Parallel Streams?</strong></p><ul><li>When processing large datasets.</li><li>When operations are independent.</li><li>When the workload benefits from parallel execution.</li></ul><p>Avoid using parallel streams blindly. They can introduce overhead, thread contention, and ordering issues. Always measure performance before choosing them.</p><h3>10. Important Things to Remember</h3><ul><li>Streams do not modify the original collection by default.</li><li>A Stream cannot be reused after a terminal operation.</li><li>Intermediate operations are lazy.</li><li>filter() is used for selecting elements.</li><li>map() is used for transforming elements.</li><li>flatMap() is used for flattening nested structures.</li><li>collect() is used for collecting results.</li><li>groupingBy() is useful for grouping records.</li><li>parallelStream() supports parallel processing, but is not always faster.</li></ul><h3>Conclusion</h3><p>Java 8 Stream API makes collection processing easier, cleaner, and more readable. It provides powerful operations such as filter(), map(), sorted(), distinct(), collect(), and groupingBy() that help developers write maintainable code.</p><p>For Java developers working with enterprise applications, Stream API is particularly useful when processing employee records, filtering orders, calculating salaries, grouping transactions, and transforming database results.</p><p>If you are learning Java 8, I recommend practicing these operations with real-world examples rather than memorizing only their definitions.</p><p><strong>Keep learning, keep coding, and keep sharing your knowledge!</strong></p><p><strong>Suggested Medium topics:</strong></p><p>Java, Programming, Software Development, Technology, Java 8</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=46321867e283" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>