Hackforge Academy

Category: React • Beginner

Published on 15 Feb 2026

Explanation

What is Java 8 Stream? Java 8 Stream is used to process collections of data (List, Set, Map, etc.) in a functional way. It helps to: •Filter data •Sort data •Convert data •Iterate data •Collect results without writing complex loops.

Code Example

Basic Syntax
collection.stream()
          .operation()
          .operation()
          .operation();
Example:
list.stream()
    .filter()
    .map()
    .collect();

Explanation

Java 8 Stream with List #white-Create List: #white-Description: •stream() → convert list to stream •forEach() → print values

Code Example

import java.util.*;
import java.util.stream.*;

public class Test {
public static void main(String[] args) {
List<String> list = Arrays.asList("Ram", 
"John", "Sam", "David");

list.stream()
.forEach(System.out::println);

    }
}

Explanation

Filter List #white-Filter names starting with "J"

Code Example

List<String> list = Arrays.asList("Ram", "John", "Sam", "Jack");

list.stream()
    .filter(name -> name.startsWith("J"))
    .forEach(System.out::println);
Output:
John
Jack
Description:
filter() → filters data based on condition

Explanation

Convert List (map)

Code Example

Convert to uppercase
List<String> list = Arrays.asList("Ram", 
"John");

list.stream()
    .map(name -> name.toUpperCase())
    .forEach(System.out::println);
Output:
RAM
JOHN
Description:
map() → transforms data

Explanation

Collect result into List

Code Example

List<String> list = Arrays.asList("Ram", "John", "Sam");

List<String> result =
        list.stream()
            .filter(name -> name.startsWith("S"))
            .collect(Collectors.toList());

System.out.println(result);
Output:
[Sam]
Description:
collect() → store result

Explanation

#white-Stream with Map: #white-Map has: •key •value #white-Use: map.entrySet().stream()

Code Example

Map<Integer, String> map = new HashMap<>();

map.put(1, "Ram");
map.put(2, "John");
map.put(3, "Sam");

map.entrySet()
   .stream()
   .forEach(entry ->
System.out.println(entry.getKey() + " " + entry.getValue()));
Output:
1 Ram
2 John
3 Sam
Description:
entrySet() → get key and value

Explanation

Filter Map

Code Example

map.entrySet()
   .stream()
   .filter(entry -> entry.getValue().startsWith("J"))
   .forEach(entry ->
       System.out.println(entry.getValue()));
Output:
John

Explanation

Convert Map values to List

Code Example

List<String> list =
        map.values()
           .stream()
           .collect(Collectors.toList());

System.out.println(list);
Output:
[Ram, John, Sam]

Explanation

Create Employee class

Code Example

Employee class
class Employee {
    int id;
    String name;
    double salary;
Employee(int id, String name, double salary) {
        this.id = id;
        this.name = name;
        this.salary = salary;
    }
public int getId() { return id; }
public String getName() { return name; }
public double getSalary() { return salary; 
}}
cont..

Explanation

Create List

Code Example

List<Employee> list = Arrays.asList(

new Employee(1, "Ram", 50000),
new Employee(2, "John", 70000),
new Employee(3, "Sam", 40000)

);

Explanation

Employee with Filter

Code Example

Filter employee salary > 50000
list.stream()
    .filter(emp -> emp.getSalary() > 50000)
    .forEach(emp ->
        System.out.println(emp.getName()));
Output:
John

Explanation

Convert List to Map

Code Example

Map<Integer, String> map =

list.stream()
    .collect(Collectors.toMap(
        Employee::getId,
        Employee::getName
));

System.out.println(map);
Output:
{1=Ram, 2=John, 3=Sam}

Explanation

#white-Important Stream Methods:

Code Example

stream()
create stream

filter()
filter data

map()
transform data

collect()
store result

Explanation

#white-Important Stream Methods:

Code Example

forEach()
iterate

sorted()
sort

count()
count

max()
maximum

min()
minimum

Explanation

#white-Before Java 8: for(String name : list){ System.out.println(name); } #white-After Java 8: list.stream() .forEach(System.out::println);

Code Example


Explanation

Use Cases #white-Used in: •Employee filtering •Database data processing •Sorting data •Converting List to Map •Removing duplicates

Code Example


Explanation

#white-Stream is used to: •Process collections •Reduce code •Improve readability •Improve performance

Code Example


Want structured learning with real projects?

Join our Weekend Live Workshop and become job-ready faster.