java8 stream interface terminal operation collect operation

Before that, we talked about the stream operation in java8, which is divided into intermediate operation and terminal operation. In terminal operation, we also saw the reduction mode. This article mainly refers to the reduction operation mode collection, collector operation and collector operation, which can be regarded as a more advanced reduction operation;

First look at the collect operation in stream

	<R> R collect(Supplier<R> supplier,BiConsumer<R, ? super T> accumulator,BiConsumer<R, R> combiner);
	<R, A> R collect(Collector<? super T, A, R> collector);

The first is an abstract method with three parameters,

The second one, which has only one parameter, first looks at the collect operation collector static factory class in stream. In this static factory class, most of the implementations are the methods of three parameters called, which almost satisfy all our daily operations. So, we only look at the implementations in this static factory class;

In the java8 actual combat, there are listed, you can see the introduction, below, we make a demonstration through the code;

package com.badger;

import java.util.ArrayList;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;

import org.junit.Test;

public class TestJava8 {
	public static List<Emp> list = new ArrayList<>();
	static {
		list.add(new Emp("Shanghai", "Pet name", 17));
		list.add(new Emp("Beijing", "Xiaohong", 18));
		list.add(new Emp("Shenzhen", "Blue", 19));
		list.add(new Emp("Guangzhou", "Small ash", 20));
		list.add(new Emp("Hangzhou", "Xiao Huang", 21));
		list.add(new Emp("Guiyang", "Xiao Bai", 22));

	public void test1() {
		// Turn list
		List<String> names = -> emp.getName()).collect(Collectors.toList());
		// Turn set
		Set<String> address = -> emp.getName()).collect(Collectors.toSet());
		// To convert to map, you need to specify key and value. Function.identity() represents the current Emp object itself
		Map<String, Emp> map =, Function.identity()));
		// Count elements
		Long count =;
		// Summing int summing long, summing double
		Integer sumAges =;
		// Average int, average double, average long
		Double aveAges =;

		// Comprehensive treatment, maximum, minimum, average, sum operation
		// summarizingInt´╝îsummarizingLong,summarizingDouble
		IntSummaryStatistics intSummary =;
		System.out.println(intSummary.getAverage());// 19.5
		System.out.println(intSummary.getMax());// 22
		System.out.println(intSummary.getMin());// 17
		System.out.println(intSummary.getSum());// 117

		// The connection string, of course, can also be overloaded, with some prefixes, suffixes and intermediate separators
		String strEmp = -> emp.getName()).collect(Collectors.joining());
		String strEmp1 = -> emp.getName()).collect(Collectors.joining("-Middle separator-"));
		String strEmp2 = -> emp.getName()).collect(Collectors.joining("-Middle separator-", "prefix*", "&Suffix"));
		System.out.println(strEmp);// Xiaoming Xiaohong Xiaolan Xiaohui Xiaohuang Xiaobai
		// Small name - middle separator - small red - middle separator - small blue - middle separator - small gray - middle separator - small yellow - middle separator - small white
		// Prefix * small name - middle separator - small red - middle separator - small blue - middle separator - small gray - middle separator - small yellow - middle separator - small white & suffix
		// maxBy swipe the maximum value according to the comparison result in the comparator
		Optional<Integer> maxAge = -> emp.getAge()).collect(Collectors.maxBy(Integer::max));
		// minimum value
		Optional<Integer> minAge = -> emp.getAge()).collect(Collectors.minBy(Integer::min));
		// Reduction operation -> emp.getAge()).collect(Collectors.reducing((x, y) -> x + y)); -> emp.getAge()).collect(Collectors.reducing(0, (x, y) -> x + y));
		// Sub operation groupingBy group the original list according to the address
		Map<String, List<Emp>> mapGroup =;
		// partitioningBy partition operation needs to judge partition according to type specification
		Map<Boolean, List<Integer>> partitioningMap = -> emp.getAge())
				.collect(Collectors.partitioningBy(emp -> emp > 20));

	static class Emp {
		private String address;

		private String name;

		private Integer age;

		public Emp() {


		public Emp(String address) {
			this.address = address;

		public Emp(String name, Integer age) { = name;
			this.age = age;

		public Emp(String address, String name, Integer age) {
			this.address = address; = name;
			this.age = age;

		public String getAddress() {
			return address;

		public void setAddress(String address) {
			this.address = address;

		public String getName() {
			return name;

		public void setName(String name) { = name;

		public Integer getAge() {
			return age;

		public void setAge(Integer age) {
			this.age = age;

		public String toString() {
			return "Emp [address=" + address + ", name=" + name + ", age=" + age + "]";

First of all, I will introduce the functional interface of java8 and the writing method of lambda expression. You can't understand it. Please refer to my previous article

Keywords: Java Junit Lambda

Added by Reviresco on Thu, 13 Feb 2020 22:34:28 +0200