Featured image of post Introduction to Java Streams API

Introduction to Java Streams API

A comprehensive guide to functional programming with Java Streams

Introduction to Java Streams API

Java 8 introduced one of the most significant features to the language: the Streams API. This powerful addition allows developers to process collections of objects in a declarative way, similar to SQL queries.

What are Streams?

Streams represent a sequence of elements and support different kinds of operations to perform computations on those elements. Operations on streams can be either intermediate or terminal.

  • Intermediate operations return a new stream and are always lazy (they don’t process the stream until a terminal operation is invoked)
  • Terminal operations produce a result or a side-effect and trigger the processing of the stream pipeline

Basic Stream Operations

Let’s look at some common operations:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
List<String> names = Arrays.asList("John", "Sarah", "Mark", "Tanya", "Robert");

// Filtering a stream
List<String> longNames = names.stream()
    .filter(name -> name.length() > 4)
    .collect(Collectors.toList());
// Result: [Sarah, Tanya, Robert]

// Mapping values
List<Integer> nameLengths = names.stream()
    .map(String::length)
    .collect(Collectors.toList());
// Result: [4, 5, 4, 5, 6]

// Finding elements
Optional<String> startsWithR = names.stream()
    .filter(name -> name.startsWith("R"))
    .findFirst();
// Result: Optional[Robert]

The Power of Method References

Java 8 introduced method references as a shorthand notation for lambda expressions:

1
2
3
4
5
// Instead of:
names.stream().map(name -> name.toUpperCase())

// You can use:
names.stream().map(String::toUpperCase)

Parallel Streams

One of the most powerful features of the Streams API is the ability to easily parallelize operations:

1
2
3
4
5
// Sequential processing
long count = names.stream().filter(name -> name.length() > 4).count();

// Parallel processing
long parallelCount = names.parallelStream().filter(name -> name.length() > 4).count();

Conclusion

The Java Streams API provides a modern and functional approach to working with collections. It improves code readability, reduces boilerplate code, and offers opportunities for optimization through parallelism.

In future posts, we’ll explore more advanced stream operations like reduce, collect, and flatMap.

Built with Hugo
Theme Stack designed by Jimmy