|
IntStream
|
for
int elements |
|
DoubleStream
|
for
double elements |
|
LongStream
|
for
long elements |
|
limit
|
.limit(5)will limit the result to the first 5 elements |
|
skip
|
.skip(5)will skip the first 5 elements |
|
filter
|
.filter(e -> e.getSalary() > 200000)will keep the elements that satisfy the given predicate. In this case, all elements that have salary above 200000 |
|
distinct
|
.distinct() will compare the elements in the stream using equals() and eliminate duplicates |
|
sorted
|
.sorted((e1, e2) -> e1.getName().compareTo(e2.getName())) will sort the elements with the given comparator. Elements must be Comparable. |
|
min
|
Similar to sorted, but it will find the min element according to the given comparator
|
|
max
|
Similar to sorted, but it will find the max element according to the given comparator
|
|
map
|
.map(employeeRepository::findById)will apply the given function and substitute the elements in the stream for new elements. In this case, it received a stream of employee IDs and returned a stream of Employee objects |
|
mapToDouble
mapToInt mapToLong |
similar to map, but the function converts the element to the specified primitive type, resulting in a specialized stream IntStream, DoubleStream or LongStream
|
|
flatMap
|
similar to map, but the number of elements resulting may be different. It's normally used to convert a List of List into a single list with all the elements
|
|
peek
|
.peek(e -> e.salaryIncrement(10.0)) will apply the give function to all elements in the list, but doesn't substitute the elements in the list |
|
reduce
|
.reduce(0.0, Double::sum) will return a single value. It starts with the identity value (0.0) and applies the given function to each element in the array. In this case it's summing all elements, one by one |
|
allMatch
|
.allMatch(i -> i % 2 == 0); will check if all elements match the given condition. If so, returns true, else returns false |
|
anyMatch
|
.anyMatch(i -> i % 2 == 0); will check if one of the elements match the given condition. If so, returns true, else returns false |
|
noneMatch
|
.noneMatch(i -> i % 2 == 0); will check if no elements match the given condition. If so, returns true, else returns false |
|
findFirst
|
.findFirst() will return an Optional with the first element in the stream |
|
forEach
|
forEach(e -> e.salaryIncrement(10.0)) will apply the given function to each element in the stream, but it's a terminal operation and returns void |
|
count
|
.count() outputs the number of elements in the stream |
|
toList
|
collect(Collectors.toList()) gather all elements in the stream into a List |
|
toSet
|
collect(Collectors.toSet()) gather all elements in the stream into a Set |
|
toCollection
|
collect(Collectors.toCollection(Vector::new)) gather all elements in the list in an arbitrary Collection |
|
joining
|
collect(Collectors.joining(", ")).toString() will join String elements with the given separator and return the aggregated String |
|
summarizingDouble
|
|
|
summaryStatistics
|
|
|
partitioningBy
|
.collect(Collectors.partitioningBy(s -> s.getGrade() >= PASS_THRESHOLD)) will partition the data into 2 categories based on the given condition. The result will be a Map<Boolean, List<Student>> |
|
groupingBy
|
.collect(Collectors.groupingBy(Employee::getDepartment)); will group the elements into categories based on the function. The result will be a Map<Department, List<Employee>> |
|
mapping
|
mapping(Person::getLastName, toSet()) it receives a function to be applied to all elements and way of collecting downstream the elements. In this case, it will get the last name of all persons and add them to a set |
|
reducing
|
|
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.