πŸ”₯ Launch Offer: Interview Preparation Bundle @ β‚Ή2,499 β‚Ή9,999 β€’ 75% OFF. Offer expires in πŸ”₯ Launch Offer: Interview Preparation Bundle @ β‚Ή2,499 β‚Ή9,999 β€’ 75% OFF. Offer expires in Interview Preparation Bundle @ β‚Ή2,499 β‚Ή9,999

00:00:00 Get Bundle
home uncategorized master-intstream-in-java-from-basics-to-real-use-case
Interview guide Uncategorized 10 sections

Master IntStream in Java: From Basics to Real Use Case

IntStream is a primitive stream in Java (from java.util.stream) that works specifically with int values. πŸ‘‰ It is part of Java 8 Streams and helps you: Avoid boxing/unboxing…

Start reading
By javainterviewprep April 12, 2026 2 min read

IntStream is a primitive stream in Java (from java.util.stream) that works specifically with int values.

πŸ‘‰ It is part of Java 8 Streams and helps you:

  • Avoid boxing/unboxing (Integer ↔ int)
  • Perform fast numeric operations like sum(), average(), max()

How IntStream is Created

βœ” Common Ways

  1. IntStream.of(...)
  2. Arrays.stream(int[])
  3. list.stream().mapToInt(...)

Complete Example (Interview Ready)

Problem:

Find the average salary of employees whose age > 30

Full Code Example

import java.util.*;
import java.util.stream.*;class Employee {
    int age;
    int salary;    Employee(int age, int salary) {
        this.age = age;
        this.salary = salary;
    }    public int getAge() {
        return age;
    }    public int getSalary() {
        return salary;
    }
}public class Main {
    public static void main(String[] args) {        List<Employee> employees = Arrays.asList(
            new Employee(25, 30000),
            new Employee(35, 50000),
            new Employee(40, 70000)
        );        double avgSalary = employees.stream()
            .filter(e -> e.getAge() > 30)     // Stream<Employee>
            .mapToInt(e -> e.getSalary())     // IntStream βœ…
            .average()                        // OptionalDouble
            .orElse(0);        System.out.println("Average Salary: " + avgSalary);
    }
}
import java.util.*;
import java.util.stream.*;class Employee {
int age;
int salary; Employee(int age, int salary) {
this.age = age;
this.salary = salary;
} public int getAge() {
return age;
} public int getSalary() {
return salary;
}
}public class Main {
public static void main(String[] args) { List<Employee> employees = Arrays.asList(
new Employee(25, 30000),
new Employee(35, 50000),
new Employee(40, 70000)
); double avgSalary = employees.stream()
.filter(e -> e.getAge() > 30) // Stream<Employee>
.mapToInt(e -> e.getSalary()) // IntStream βœ…
.average() // OptionalDouble
.orElse(0); System.out.println("Average Salary: " + avgSalary);
}
}

Step-by-Step Flow

  1. employees.stream()
    β†’ Stream<Employee>
  2. .filter(e -> e.getAge() > 30)
    β†’ still Stream<Employee>
  3. .mapToInt(e -> e.getSalary())
    β†’ πŸ”₯ converts to IntStream
  4. .average()
    β†’ returns OptionalDouble
  5. .orElse(0)
    β†’ gives final double

Output

Average Salary: 60000.0

Another Simple Example (Very Important)

IntStream numbers = IntStream.of(10, 20, 30, 40);int sum = numbers.sum();System.out.println(sum); // 100

Key Interview Points

  • IntStream is for primitive int values
  • Created using:
    • mapToInt()
    • IntStream.of()
    • Arrays.stream()
  • Supports operations:
    • sum(), average(), max(), min()

Final One-Line Answer (Interview)

β€œIntStream is a primitive stream of integers used for efficient numeric operations, typically created using mapToInt() when converting from object streams.”