1. The allMatch() operation
2. The anyMatch() operation
3. The noneMatch() operation
4. The collect() operation
5. The count() operation
6. The forEach() operation
7. The min() operation
8. The max() operation
9. The reduce() operation
boolean areAllMale = listPersons.stream() .allMatch(p -> p.getGender().equals(Gender.MALE)); System.out.println("Are all persons male? " + areAllMale);Result:
Are all persons male? falseThe following example check if all people using GMail:
boolean useGMail = listPersons.stream() .allMatch(p -> p.getEmail().endsWith("gmail.com")); System.out.println("Does everyone use GMail? " + useGMail);
Does everyone use GMail? trueNote that if the stream is empty, the method returns true and the predicate is not evaluated.
boolean anyFemale = listPersons.stream() .anyMatch(p -> p.getGender().equals(Gender.FEMALE)); System.out.println("Is there any female? " + anyFemale);Result:
Is there any female? trueThe following example checks whether the list has any teenager (age under 20):
boolean anyTeenager = listPersons.stream() .anyMatch(p -> p.getAge() < 20); System.out.println("Is there any teenager? " + anyTeenager);Output:
Is there any teenager? false
boolean noYahooMail = listPersons.stream() .noneMatch(p -> p.getEmail().endsWith("yahoo.com")); System.out.println("No one uses Yahoo mail? " + noYahooMail);Result:
No one uses Yahoo mail? trueThe following example answers the question: Does anyone come from the Hill family?
boolean noHill = listPersons.stream() .noneMatch(p -> p.getLastName().equals("Hill")); System.out.println("No one comes from Hill family? " + noHill);Output:
No one comes from Hill family? false
List<String> listEmails = listPersons.stream() .map(p -> p.getEmail()) .collect(Collectors.toList()); System.out.println("List of Emails: " + listEmails);Output:
List of Emails: [alice@gmail.com, bob@gmail.com, carol@gmail.com, david@gmail.com, eric@gmail.com, frank@gmail.com, gibb@gmail.com, henry@gmail.com, isabell@gmail.com, jane@gmail.com]We can specify exactly which type of collection as the result. For example, the following code collects emails into a TreeSet:
Set<String> setEmails = listPersons.stream() .map(p -> p.getEmail()) .collect(Collectors.toCollection(TreeSet::new));Output:
Set of Emails: [alice@gmail.com, bob@gmail.com, carol@gmail.com, david@gmail.com, eric@gmail.com, frank@gmail.com, gibb@gmail.com, henry@gmail.com, isabell@gmail.com, jane@gmail.com]The following example groups the person by gender:
Map<Gender, List<Person>> byGender = listPersons.stream() .collect(Collectors.groupingBy(p -> p.getGender())); System.out.println("Groups by gender:\n" + byGender);Output:
{FEMALE=[Alice Brown, Carol Hill, Isabell Hill, Jane Smith], MALE=[Bob Young, David Green, Eric Young, Frank Thompson, Gibb Brown, Henry Baker]}The following example accumulates first names and concatenates them into a String, separated by commas:
String firstNames = listPersons.stream() .map(p -> p.getFirstName()) .collect(Collectors.joining(", "));Result:
First Names: Alice, Bob, Carol, David, Eric, Frank, Gibb, Henry, Isabell, JaneConsult the Collectors Javadoc for more useful mutable reduction operations.
long totalMale = listPersons.stream() .filter(p -> p.getGender().equals(Gender.MALE)) .count(); System.out.println("Total male: " + totalMale);Output:
Total male: 6You can read Java 8 in Action book to learn more in-depth about Java Stream.
System.out.println("People are female:"); listPersons.stream() .filter(p -> p.getGender().equals(Gender.FEMALE)) .forEach(System.out::println);Output:
People are female: Alice Brown Carol Hill Isabell Hill Jane Smith
Optional<Person> min = listPersons.stream() .filter(p -> p.getGender().equals(Gender.FEMALE)) .min((p1, p2) -> p1.getAge() - p2.getAge()); if (min.isPresent()) { Person youngestGirl = min.get(); System.out.println("The youngest girl is: " + youngestGirl + " (" + youngestGirl.getAge() + ")"); }Output:
The youngest girl is: Isabell Hill (22)
Optional<Person> max = listPersons.stream() .filter(p -> p.getGender().equals(Gender.MALE)) .max((p1, p2) -> p1.getAge() - p2.getAge()); if (max.isPresent()) { Person oldestMan = max.get(); System.out.println("The oldest man is: " + oldestMan + " (" + oldestMan.getAge() + ")"); }Result:
The oldest man is: David Green (39)
Optional<String> reducedValue = listPersons.stream() .map(p -> p.getFirstName()) .reduce((name1, name2) -> name1 + ", " + name2); if (reducedValue.isPresent()) { String names = reducedValue.get(); System.out.println(names); }Output:
Alice, Bob, Carol, David, Eric, Frank, Gibb, Henry, Isabell, Jane
int[] numbers = {123, 456, 789, 246, 135, 802, 791}; int sum = Arrays.stream(numbers).reduce(0, (x, y) -> (x + y)); System.out.println("sum = " + sum);Output:
sum = 3342
U reduce(U identity,
BiFunction<U,? super T,U> accumulator,
BinaryOperator<U> combiner)
This is the most general reduction method that performs a reduction on elements of the stream, using the provided identity, accumulator and combiner.The identity element is both an initial seed value for the reduction and a default result if there are no input elements.The accumulator function takes a partial result and the next element, and produces a new partial resultThe combiner function combines two partial results to produce a new partial result (it is necessary in parallel reductions).The following example shows how this general reduction operation is used to accumulate numbers to calculate sum of them:int[] numbers = {123, 456, 789, 246, 135, 802, 791}; int sum = Arrays.stream(numbers).reduce(0, (x, y) -> (x + y), Integer::sum); System.out.println("sum = " + sum);That’s all about common terminal operations provided by the Stream API. For in-depth information about stream operations, see its Javadoc or read this book.