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 java.util.stream.Collectors;

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));
	}

	@Test
	public void test1() {
		// Turn list
		List<String> names = list.stream().map(emp -> emp.getName()).collect(Collectors.toList());
		// Turn set
		Set<String> address = list.stream().map(emp -> 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 = list.stream().collect(Collectors.toMap(Emp::getName, Function.identity()));
		// Count elements
		Long count = list.stream().collect(Collectors.counting());
		// Summing int summing long, summing double
		Integer sumAges = list.stream().collect(Collectors.summingInt(Emp::getAge));
		// Average int, average double, average long
		Double aveAges = list.stream().collect(Collectors.averagingInt(Emp::getAge));

		// Comprehensive treatment, maximum, minimum, average, sum operation
		// summarizingInt´╝îsummarizingLong,summarizingDouble
		IntSummaryStatistics intSummary = list.stream().collect(Collectors.summarizingInt(Emp::getAge));
		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 = list.stream().map(emp -> emp.getName()).collect(Collectors.joining());
		String strEmp1 = list.stream().map(emp -> emp.getName()).collect(Collectors.joining("-Middle separator-"));
		String strEmp2 = list.stream().map(emp -> 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
		System.out.println(strEmp1);
		// Prefix * small name - middle separator - small red - middle separator - small blue - middle separator - small gray - middle separator - small yellow - middle separator - small white & suffix
		System.out.println(strEmp2);
		// maxBy swipe the maximum value according to the comparison result in the comparator
		Optional<Integer> maxAge = list.stream().map(emp -> emp.getAge()).collect(Collectors.maxBy(Integer::max));
		// minimum value
		Optional<Integer> minAge = list.stream().map(emp -> emp.getAge()).collect(Collectors.minBy(Integer::min));
		// Reduction operation
		list.stream().map(emp -> emp.getAge()).collect(Collectors.reducing((x, y) -> x + y));
		list.stream().map(emp -> 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 = list.stream().collect(Collectors.groupingBy(Emp::getAddress));
		// partitioningBy partition operation needs to judge partition according to type specification
		Map<Boolean, List<Integer>> partitioningMap = list.stream().map(emp -> 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) {
			this.name = name;
			this.age = age;
		}

		public Emp(String address, String name, Integer age) {
			super();
			this.address = address;
			this.name = 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) {
			this.name = name;
		}

		public Integer getAge() {
			return age;
		}

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

		@Override
		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