Common Operations

Collect

Store in a list:

List<String> list;

list = strings.stream().collect(Collectors.toList());

Store in a string:

String string;

string = strings.stream().collect(Collectors.joining(", "));

Store in a map:

Map<String, Named> map;

map  = objs.stream().collect(Collectors.toMap(Named::getName, Function.identity()));

Filter

Remove values.

Collection<Object> collection;
Collection<Object> filtered;

// Collection is initialized

// Removes null values
filtered = collection.stream().filter(Objects::nonNull).collect(Collectors.toList());

Map

Transform one object into another.

Flat Map

Transform into an stream and merge.

Reduce

Combines all the values.

Some streams include the most common reduction functions:

Consume (Apply to All)

Apply a function to all the elements.

Concatenate

Combine streams.

Last updated

Was this helpful?