Java教程

Java8 Collectors

Collectors是扩展Object类的最终类。它提供了归约操作,例如将元素累积到集合中,根据各种标准对元素进行汇总等。
Java Collectors类提供了各种处理元素的方法
方法 说明
public static <T> Collector<T,?,Double> averagingDouble(ToDoubleFunction<? super T> mapper) 它返回一个收集器,该收集器产生应用于输入元素的双值函数的算术平均值。如果不存在任何元素,则结果为0。
public static <T> Collector<T,?,T> reducing(T identity, BinaryOperator<T> op) 它返回一个收集器,该收集器使用提供的标识在指定的BinaryOperator下执行其输入元素的缩减。
public static <T> Collector<T,?,Optional<T>> reducing(BinaryOperator<T> op) 它返回一个Collector,该Collector在指定的BinaryOperator下执行其输入元素的缩减。结果描述为Optional <T>。
public static <T,U> Collector<T,?,U> reducing(U identity, Function<? super T,? extends U> mapper, BinaryOperator<U> op) 它返回一个Collector,该Collector在指定的映射函数和BinaryOperator下执行其输入元素的缩减。这是reduce(Object,BinaryOperator)的概括,它允许在归约之前对元素进行转换。
public static <T,K> Collector<T,?,Map<K,List<T>>> groupingBy(Function<? super T,? extends K> classifier) 它返回一个收集器,该收集器对T类型的输入元素实施"分组依据"操作,根据分类函数对元素进行分组,并将结果返回到Map中。
public static <T,K,A,D> Collector<T,?,Map<K,D>> groupingBy(Function<? super T,? extends K> classifier, Collector<? Super T,A,D> downstream) 它返回一个收集器,对类型T的输入元素实施级联的"分组依据"操作,根据分类函数对元素进行分组,然后使用指定的下游收集器对与给定键关联的值执行归约操作。
public static <T,K,D,A,M extends Map<K,D>> Collector<T,?,M> groupingBy(Function<? super T,? extends K> classifier, Supplier mapFactory, Collector<? super T,A,D> downstream) 它返回一个收集器,对类型T的输入元素实施级联的"分组依据"操作,根据分类函数对元素进行分组,然后使用指定的下游收集器对与给定键关联的值执行归约操作。收集器生成的地图是使用提供的工厂功能创建的。
public static <T,K> Collector<T,?,ConcurrentMap<K,List<T>>> groupingByConcurrent(Function<? super T,? extends K> classifier) 它返回并发的收集器,该收集器对T类型的输入元素实施"分组依据"操作,并根据分类函数对元素进行分组。
public static <T,K,A,D> Collector<T,?,ConcurrentMap<K,D>> groupingByConcurrent(Function<? super T,? extends K> classifier, Collector<? super T,A,D> downstream) 它返回并发的收集器,对类型T的输入元素实施级联的"分组依据"操作,根据分类函数对元素进行分组,然后使用指定的下游收集器对与给定键关联的值执行归约操作。
public static <T,K,A,D,M extends ConcurrentMap<K,D>> Collector<T,?,M> groupingByConcurrent(Function<? super T,? extends K> classifier, Supplier mapFactory, Collector<? super T,A,D> downstream) 它返回并发的收集器,对类型T的输入元素实施级联的"分组依据"操作,根据分类函数对元素进行分组,然后使用指定的下游收集器对与给定键关联的值执行归约操作。收集器生成的ConcurrentMap是使用提供的工厂函数创建的。
public static <T> Collector<T,?,Map<Boolean,List<T>>> partitioningBy(Predicate<? super T> predicate) 它返回一个收集器,该收集器根据谓词对输入元素进行分区,并将它们组织成Map<Boolean,List <T>>。无法保证返回的Map的类型,可变性,可序列化性或线程安全性。
public static <T,D,A> Collector<T,?,Map<Boolean,D>> partitioningBy(Predicate<? super T> predicate, Collector<? Super T,A,D> downstream) 它返回一个收集器,该收集器根据谓词对输入元素进行分区,根据另一个收集器对每个分区中的值进行归约,然后将它们组织成Map<Boolean,D>,其值是下游归约的结果。/td>
public static <T,K,U> Collector<T,?,Map<K,U>> toMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper) 它返回一个收集器,该收集器将元素累积到Map中,其键和值是将提供的映射函数应用于输入元素的结果。
public static <T,K,U> Collector<T,?,Map<K,U>> toMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper, BinaryOperator<U> mergeFunction) 它返回一个收集器,该收集器将元素累积到Map中,其键和值是将提供的映射函数应用于输入元素的结果。
public static <T,K,U,M extends Map<K,U>> Collector<T,?,M> toMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper, BinaryOperator<U> mergeFunction, Supplier mapSupplier) 它返回一个收集器,该收集器将元素累积到Map中,其键和值是将提供的映射函数应用于输入元素的结果。
public static <T,K,U> Collector<T,?,ConcurrentMap<K,U>> toConcurrentMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper) 它返回一个并发的收集器,该收集器将元素累积到ConcurrentMap中,其键和值是将提供的映射函数应用于输入元素的结果。
public static <T,K,U> Collector<T,?,ConcurrentMap<K,U>> toConcurrentMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper, BinaryOperator<U> mergeFunction) 它返回一个并发的收集器,该收集器将元素累积到ConcurrentMap中,其键和值是将提供的映射函数应用于输入元素的结果。
public static <T,K,U,M extends ConcurrentMap<K,U>> Collector<T,?,M> toConcurrentMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper, BinaryOperator<U> mergeFunction, Supplier mapSupplier) 它返回一个并发的收集器,该收集器将元素累积到ConcurrentMap中,其键和值是将提供的映射函数应用于输入元素的结果。
public static <T> Collector<T,?,IntSummaryStatistics> summarizingInt(ToIntFunction<? super T> mapper) 它返回一个收集器,该收集器将一个生成int的映射函数应用于每个输入元素,并返回结果值的摘要统计信息。
public static <T> Collector<T,?,LongSummaryStatistics> summarizingLong(ToLongFunction<? super T> mapper) 它返回一个收集器,该收集器对每个输入元素应用长时间生成的映射函数,并返回结果值的摘要统计信息。
public static <T> Collector<T,?,DoubleSummaryStatistics> summarizingDouble(ToDoubleFunction<? super T> mapper) 它返回一个收集器,该收集器对每个输入元素应用双重生成的映射函数,并返回结果值的摘要统计信息。

Java收集器示例: 以列表形式获取数据

import java.util.stream.Collectors;
import java.util.List;
import java.util.ArrayList;
class Product{
	int id;
	String name;
	float price;
	
	public Product(int id, String name, float price) {
		this.id = id;
		this.name = name;
		this.price = price;
	}
}
public class CollectorsExample {
	public static void main(String[] args) {
		List<Product> productsList = new ArrayList<Product>();
		//Adding Products
		productsList.add(new Product(1,"HP Laptop",25000f));
		productsList.add(new Product(2,"Dell Laptop",30000f));
		productsList.add(new Product(3,"Lenevo Laptop",28000f));
		productsList.add(new Product(4,"Sony Laptop",28000f));
		productsList.add(new Product(5,"Apple Laptop",90000f));
		List<float> productPriceList = 
				productsList.stream()
							.map(x->x.price)			// fetching price
							.collect(Collectors.toList());	// collecting as list
		System.out.println(productPriceList);
	}
}
输出:
[25000.0, 30000.0, 28000.0, 28000.0, 90000.0]

Java收集器示例: 将数据转换为一组

import java.util.stream.Collectors;
import java.util.Set;
import java.util.List;
import java.util.ArrayList;
classProduct{
	intid;
	String name;
	floatprice;
	
	public Product(intid, String name, floatprice) {
		this.id = id;
		this.name = name;
		this.price = price;
	}
}
publicclass CollectorsExample {
	publicstaticvoid main(String[] args) {
		List<Product>productsList = new ArrayList<Product>();
		//Adding Products
		productsList.add(newProduct(1,"HP Laptop",25000f));
		productsList.add(newProduct(2,"Dell Laptop",30000f));
		productsList.add(newProduct(3,"Lenevo Laptop",28000f));
		productsList.add(newProduct(4,"Sony Laptop",28000f));
		productsList.add(newProduct(5,"Apple Laptop",90000f));
		Set<float>productPriceList = 
				productsList.stream()
							.map(x->x.price)			// fetching price
							.collect(Collectors.toSet());	// collecting as list
		System.out.println(productPriceList);
	}
}
输出:
[25000.0, 30000.0, 28000.0, 90000.0]

Java收集器示例: 使用sum方法

import java.util.stream.Collectors;
import java.util.List;
import java.util.ArrayList;
class Product{
	int id;
	String name;
	float price;
	
	public Product(int id, String name, float price) {
		this.id = id;
		this.name = name;
		this.price = price;
	}
}
public class CollectorsExample {
	public static void main(String[] args) {
		List<Product> productsList = new ArrayList<Product>();
		//Adding Products
		productsList.add(new Product(1,"HP Laptop",25000f));
		productsList.add(new Product(2,"Dell Laptop",30000f));
		productsList.add(new Product(3,"Lenevo Laptop",28000f));
		productsList.add(new Product(4,"Sony Laptop",28000f));
		productsList.add(new Product(5,"Apple Laptop",90000f));
		Double sumPrices = 
				productsList.stream()
							.collect(Collectors.summingDouble(x->x.price));	// collecting as list
		System.out.println("Sum of prices: "+sumPrices);
		Integer sumId = 
				productsList.stream().collect(Collectors.summingInt(x->x.id));
		System.out.println("Sum of id's: "+sumId);
	}
}
输出:
Sum of prices: 201000.0
Sum of id's: 15

Java收集器示例: 获取产品平均价格

import java.util.stream.Collectors;
import java.util.List;
import java.util.ArrayList;
class Product{
	int id;
	String name;
	float price;
	
	public Product(int id, String name, float price) {
		this.id = id;
		this.name = name;
		this.price = price;
	}
}
public class CollectorsExample {
	public static void main(String[] args) {
		List<Product> productsList = new ArrayList<Product>();
		//Adding Products
		productsList.add(new Product(1,"HP Laptop",25000f));
		productsList.add(new Product(2,"Dell Laptop",30000f));
		productsList.add(new Product(3,"Lenevo Laptop",28000f));
		productsList.add(new Product(4,"Sony Laptop",28000f));
		productsList.add(new Product(5,"Apple Laptop",90000f));
		Double average = productsList.stream()
						 .collect(Collectors.averagingDouble(p->p.price));
		System.out.println("Average price is: "+average);
	}
}
输出:
Average price is: 40200.0

Java收集器示例: 计数元素

import java.util.stream.Collectors;
import java.util.List;
import java.util.ArrayList;
class Product{
	intid;
	String name;
	floatprice;
	
	public Product(intid, String name, floatprice) {
		this.id = id;
		this.name = name;
		this.price = price;
	}
	publicint getId() {
		returnid;
	}
	public String getName() {
		returnname;
	}
	publicfloat getPrice() {
		returnprice;
	}
}
publicclass CollectorsExample {
	publicstaticvoid main(String[] args) {
		List<Product>productsList = new ArrayList<Product>();
		//Adding Products
		productsList.add(new Product(1,"HP Laptop",25000f));
		productsList.add(new Product(2,"Dell Laptop",30000f));
		productsList.add(new Product(3,"Lenevo Laptop",28000f));
		productsList.add(new Product(4,"Sony Laptop",28000f));
		productsList.add(new Product(5,"Apple Laptop",90000f));
		long noOfElements = productsList.stream()
							   .collect(Collectors.counting());
		System.out.println("Total elements : "+noOfElements);
	}
}
输出:
Total elements : 5
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4