Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Streams API — Filter, Map, Reduce

Lesson 38 of 39 in the free Java notes on Siksha Sarovar, written by Rohit Jangra.

What is a Stream?

A Stream is a pipeline of operations on a sequence of elements. It does not store data — it computes on demand. Streams are great for expressing "do X to every element, keep the ones that match Y, then reduce to one value" without writing explicit loops.

Three kinds of operations

  1. Sourcelist.stream(), Arrays.stream(arr), Stream.of(...).
  2. Intermediate — return another stream. Lazy. filter, map, flatMap, sorted, distinct, limit, skip.
  3. Terminal — trigger the pipeline. collect, forEach, count, reduce, anyMatch, findFirst.

Common Recipes

Filter + Map + Collect

List<String> names = people.stream()
    .filter(p -> p.age >= 18)
    .map(p -> p.name.toUpperCase())
    .collect(Collectors.toList());

Sum / Reduce

int total = nums.stream().mapToInt(Integer::intValue).sum();
int product = nums.stream().reduce(1, (a, b) -> a * b);

Grouping

Map<String, List<Student>> byBranch =
    students.stream().collect(Collectors.groupingBy(s -> s.branch));

Rules of Thumb

• Streams are single-use — once a terminal op runs, you can't reuse it. • Prefer streams when the data flow is clear; stick with for loops for stateful logic with side effects. • Use parallelStream() only after measuring — it isn't always faster.