3.1 KiB
3.1 KiB
title |
---|
Streams |
Streams
In Java 8 Streams got added as a new feature the Java toolbox. Streams allow you to quickly and cleanly process Collections.
Please read the chapter about lambdas and functional programming before continuing.
How it works
The Stream loops the elements of the collection for you. Each intermediate and the terminal operation are called for each object. Once all operations are finished for the first objects, then the second object is loaded.
Important Methods
Creation
Collection.stream()
: create a Stream from any object from any class implementingCollection
Arrays.stream(array)
: create a Stream from an Array
Intermediate Operations
These operations convert the Stream Objects somehow.
Stream.map(Function<In,Out> function)
: apply a Function to convert In to OutStream.filter(Predicate<In> predicate)
: remove Objects from Stream for which the Predicate does not test trueStream.distinct()
: remove Objects from Stream which are duplicatesStream.sorted()
: sort the Objects in the StreamStream.limit(int n)
: end Stream after n Objects
Terminal Operations
These operations receive the Stream Objects and end the Stream.
Stream.collect(Collector<In,?,Out> collector)
: collect all Objects in Stream into ObjectStream.forEach(Consumer<In> consumer)
: consume all Objects in Stream using the consumer functionStream.count()
: count all Objects in StreamStream.findFirst()
: return the first Object of the Stream and stopStream.anyMatch(Predicate<In> predicate)
: return true if any Object in the Stream tests true for the PredicateStream.allMatch(Predicate<In> predicate)
: return true if all Object in the Stream test true for the Predicate
Examples
// print the length of all Strings in a List
for (String string : Arrays.asList("abc", "de", "f", "abc")) {
int length = string.length();
System.out.println(length);
}
Arrays.asList("abc", "de", "f", "abc")
.stream()
.map(String::length)
.forEach(System.out::println);
// output: 3 2 1 3
// print all Strings in a List with a Length greater than 2
for (String string : Arrays.asList("abc", "de", "f", "abc")) {
if (string.length() > 2) {
System.out.println(string);
}
}
Arrays.asList("abc", "de", "f", "abc")
.stream()
.filter(string -> string.length() > 2)
.forEach(System.out::println);
// output: abc abc
// create a sorted List with all unique Strings from another List which are longer than or requal 2
List<String> result = new ArrayList<>();
for (String string : Arrays.asList("de", "abc", "f", "abc")) {
if (string.length() >= 2
&& ! result.contains(string)) {
result.add(string);
}
}
Collections.sort(result);
List<String> result2 = Arrays.asList("de", "abc", "f", "abc")
.stream()
.filter(string -> string.length() >= 2)
.distinct()
.sorted()
.collect(Collectors.toList());
// result: abc de