Example on a basic Array:
private int[] numbers = new int[]{1,4,5,3,2};
private String[] names = new String[]{"Jay","Paul"};numbers = Arrays.stream(numbers).sorted().toArray();-
if every object of the stream implements the Comparably interface, you can just call
sorted()and it will sort the stream according to thecompareTofunction. -
if this is not the case, or you simply want another sorting criteria, the static fuction
Comparator.comparingcan be used to specify how to sort.
names = Arrays.stream(names)
.sorted(Comparator.comparing(s -> s.charAt(0))).toArray(String[]::new);- here the Array was changed to a
String Array, and sorted according to the first char of each String;
- the
takeWhile()operation takes apredicateas an parameter, and breaks the stream, at the first element that does not satisfy the condition.
Arrays.stream(numbers).takeWhile(number -> number < 10);- the calculation of the stream ends with the first
numberthats less than 10 (excluded)
- the
dropWhile()operation is basically the opposite oftakeWhile, it takes apredicateas an parameter, and removes elements from the stream until the first element does not satisfy the condition.
int[] numbers = new int[] {3,4,5,11,6,20};
Arrays.stream(numbers).dropWhile(number -> number < 10);- in this example, only the elements
6and20will remain for further calculations.
-
AnyMatch(predicate): returs true if one element satisfies the predicate -
AllMatch(predicate): returns true if all elements satisfy the predicate -
NoneMatch(predicate): returns true if no element satisfies the predicate -
count(): returns the number of elements in the stream -
distinct(): returns a stream where each distinct element only occures once. Uses theHashCodeto distinct between objects -
...
- the function mapps the current stream element into a new type
Arrays.stream(names).map(string -> string.charAt(0));- in this example all strings of the stream are mapped to their first char -> a char stream is returned.
public static String prettyDirections(Stream<OneWay> oneWays) {
return oneWays.map(OneWay::prettyPrint).collect(Collectors.joining("\n"));
}
// Collectors.joining(...) will join all elements of stream to a string seperated by specifyed delimitertrain ex from pgdp
Map<String, List<TrainConnection>> typeToConnections = connections
.collect(Collectors.groupingBy(TrainConnection::type));Variation of function above where you pass a second argument (Collector) in .groupingBy() to get e.g. an average.
connections.flatMap(trainConnection -> trainConnection.stops().stream()).collect(Collectors.groupingBy(trainStop -> trainStop.actual().getHour(), Collectors.averagingDouble(TrainStop::getDelay)));this would be the second Collector in this example
Collectors.averagingDouble(TrainStop::getDelay)- per default, the elements in a stream are always objects, if you want to apply a functions specific to primitve types, a conversion is required.
Stream tmp = Arrays.stream(names).mapToInt(string -> string.length());
return tmp.sum();-
this converts the stream into a stream of primitive types and sums each value.
-
this also works for
doubleandlong
the flatmMap operation takes a steam of streams as input, and converts it into a single output stream.