Understanding Java Stream API: filter, map, flatMap, and Parallel Operations

This article introduces Java's Stream API, explaining how filter, map, flatMap, and other intermediate operations work, provides practical code examples for each, and demonstrates stream creation, conversion, and parallel processing to efficiently handle collections and large data sets.

Architect's Tech Stack
Architect's Tech Stack
Architect's Tech Stack
Understanding Java Stream API: filter, map, flatMap, and Parallel Operations

Introduction to Java Stream API, explaining that streams provide a unified way to process collections and large data efficiently.

Demonstrates filter operation with example code showing how to filter a list of maps by non‑null IP values.

list.stream().filter(smap -> null != smap.get("ip") && !"".equals(smap.get("ip"))).forEach(imp -> { listipzone.add(wry.findIP(imp.get("ip").toString())); });

Shows the definition of Stream.filter(Predicate<? super T> predicate) and a simple example converting an array to a stream and filtering for the element "a".

Explains the map operation, its signature Stream<R> map(Function<? super T, ? extends R> mapper), and provides examples that convert integers to strings and extract the name field from a custom Emp object.

public static void main(String[] args) { Integer[] dd = {1,2,3}; Stream<Integer> stream = Arrays.stream(dd); stream.map(str -> Integer.toString(str)).forEach(System.out::println); List<Emp> list = Arrays.asList(new Emp("a"), new Emp("b"), new Emp("c")); list.stream().map(emp -> emp.getName()).forEach(System.out::println); }

Introduces flatMap, its signature, and a sample that splits strings into character arrays and then flattens them into a single stream.

String[] strs = {"aaa","bbb","ccc"}; Arrays.stream(strs).map(str -> str.split("")).flatMap(Arrays::stream).forEach(System.out::println);

Lists common ways to create streams (Stream.of, Arrays.stream, collection.stream) and how to convert streams back to arrays, lists, sets, strings, or maps using collectors.

Describes intermediate operations (map, filter, distinct, sorted, peek, limit, skip, parallel, etc.) and terminal operations (forEach, collect, reduce, min, max, count, match, findFirst, etc.).

Shows usage of limit and skip together with mapToInt and peek to process a list of integers.

List<Integer> personList2 = persons.stream().map(Person::getName).limit(10).skip(3).collect(Collectors.toList());

Explains parallel streams: calling parallel() on an existing stream or using parallelStream() on a collection to leverage multi‑core CPUs.

int sumSize = Stream.of("Apple","Banana","Orange","Pear").parallel().map(s -> s.length()).reduce(Integer::sum).get();
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend DevelopmentLambdafunctional programmingStream APIParallelism
Architect's Tech Stack
Written by

Architect's Tech Stack

Java backend, microservices, distributed systems, containerized programming, and more.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.