个性化阅读
专注于IT技术分析

Java收集器

点击下载

收集器是扩展Object类的最终类。它提供归约操作, 例如将元素累积到集合中, 根据各种标准对元素进行汇总等。

Java Collectors类提供了各种处理元素的方法

方法 描述
公共静态<T>收集器<T, ?, Double> averagingDouble(ToDoubleFunction <?super T>映射器) 它返回一个收集器, 该收集器产生应用于输入元素的双值函数的算术平均值。如果不存在任何元素, 则结果为0。
公共静态<T>收集器<T, ?, T>减少(T身份, BinaryOperator <T> op) 它返回一个收集器, 该收集器使用提供的标识在指定的BinaryOperator下执行其输入元素的缩减。
公共静态<T>收集器<T, ?, 可选<T >>精简(BinaryOperator <T> op) 它返回一个收集器, 该收集器在指定的BinaryOperator下执行其输入元素的缩减。结果描述为Optional <T>。
公共静态<T, U> Collector <T, ?, U>减少(U标识, Function <?超级T , ?扩展U>映射器, BinaryOperator <U> op) 它返回一个收集器, 该收集器在指定的映射函数和BinaryOperator下执行其输入元素的缩减。这是reduce(Object, BinaryOperator)的一般化, 它允许在还原之前对元素进行转换。
公共静态<T, K> Collector <T, ?, Map <K, List <T >>> groupingBy(Function <?super T, ?extended K>分类器) 它返回一个收集器, 对类型T的输入元素实施“分组依据”操作, 根据分类函数对元素进行分组, 然后将结果返回到Map中。
公共静态<T, K, A, D> Collector <T, ?, Map <K, D >> groupingBy(功能<?super T , ?扩展K>分类器, Collector <?Super T, A, D>下游) 它返回一个收集器, 对类型T的输入元素实施级联的“分组依据”操作, 根据分类函数对元素进行分组, 然后使用指定的下游收集器对与给定键关联的值执行归约操作。
公共静态<T, K, D, A, M扩展了Map <K, D >> Collector <T, ?, M> groupingBy(Function <?super T , ?扩展了K>分类器, Supplier <M> mapFactory, Collector < ?超级T, A, D>下游) 它返回一个收集器, 对类型T的输入元素实施级联的“分组依据”操作, 根据分类函数对元素进行分组, 然后使用指定的下游收集器对与给定键关联的值执行归约操作。收集器生成的地图是使用提供的工厂功能创建的。
公共静态<T, K>收集器<T, ?, ConcurrentMap <K, List <T >>> groupingByConcurrent(Function <?super T, ?extended K>分类器) 它返回并发的收集器, 对类型T的输入元素实施“分组依据”操作, 并根据分类功能对元素进行分组。
公共静态<T, K, A, D> Collector <T, ?, ConcurrentMap <K, D >> groupingByConcurrent(Function <?super T , ?扩展K>分类器, Collector <?super T, A, D>下游) 它返回并发的收集器, 对类型T的输入元素实施级联的“分组依据”操作, 根据分类功能对元素进行分组, 然后使用指定的下游收集器对与给定键关联的值执行归约操作。
公共静态<T, K, A, D, M扩展ConcurrentMap <K, D >> Collector <T, ?, M> groupingByConcurrent(Function <?super T , ?扩展K>分类器, Supplier <M> mapFactory, Collector < ?超级T, A, D>下游) 它返回并发的收集器, 对类型T的输入元素实施级联的“分组依据”操作, 根据分类功能对元素进行分组, 然后使用指定的下游收集器对与给定键关联的值执行归约操作。收集器生成的ConcurrentMap是使用提供的工厂函数创建的。
公共静态<T>收集器<T, ?, Map <布尔值, 列表<T >>> partitioningBy(Predicate <?super T>谓词) 它返回一个收集器, 该收集器根据谓词对输入元素进行分区, 并将它们组织成Map <Boolean, List <T >>。不能保证返回的Map的类型, 可变性, 可序列化性或线程安全性。
公共静态<T, D, A> Collector <T, ?, Map <Boolean, D >> partitioningBy(Predicate <?super T>谓词, Collector <?Super T, A, D>下游) 它返回一个收集器, 该收集器根据谓词对输入元素进行分区, 根据另一个收集器对每个分区中的值进行归约, 然后将它们组织成Map <Boolean, D>, 其值是下游归约的结果。
公共静态<T, K, U> Collector <T, ?, Map <K, U >> toMap(Function <?super T, ?extended K> keyMapper, Function <?super T, ?extended U> valueMapper) 它返回一个收集器, 该收集器将元素累积到Map中, 其键和值是将提供的映射函数应用于输入元素的结果。
公共静态<T, K, U> Collector <T, ?, Map <K, U >> toMap(Function <?super T, ?extended K> keyMapper, Function <?super T, ?extended U> valueMapper, BinaryOperator < U> mergeFunction) 它返回一个收集器, 该收集器将元素累积到Map中, 其键和值是将提供的映射函数应用于输入元素的结果。
公共静态<T, K, U, M将Map <K, U >> Collector <T, ?, M>扩展到Map(Function <?super T , ?扩展K> keyMapper, Function <?super T , ?扩展U> valueMapper, BinaryOperator <U> mergeFunction, 供应商<M> mapSupplier) 它返回一个收集器, 该收集器将元素累积到Map中, 其键和值是将提供的映射函数应用于输入元素的结果。
公共静态<T, K, U> Collector <T, ?, ConcurrentMap <K, U >> toConcurrentMap(Function <?super T, ?extended K> keyMapper, Function <?super T, ?extended U> valueMapper) 它返回一个并发的收集器, 该收集器将元素累积到ConcurrentMap中, 其键和值是将提供的映射函数应用于输入元素的结果。
public static <T, K, U> Collector <T, ?, ConcurrentMap <K, U >> toConcurrentMap(Function <?super T, ?extended K> keyMapper, Function <?super T, ?extended U> valueMapper, BinaryOperator < U> mergeFunction) 它返回一个并发的收集器, 该收集器将元素累积到ConcurrentMap中, 其键和值是将提供的映射函数应用于输入元素的结果。
公共静态<T, K, U, M将ConcurrentMap <K, U >>收集器<T, ?, M>扩展为ConcurrentMap(Function <?super T , ?扩展K> keyMapper, Function <?super T , ?扩展U> valueMapper, BinaryOperator <U> mergeFunction, 供应商<M> mapSupplier) 它返回一个并发的收集器, 该收集器将元素累积到ConcurrentMap中, 其键和值是将提供的映射函数应用于输入元素的结果。
公共静态<T>收集器<T, ?, IntSummaryStatistics> summarizingInt(ToIntFunction <?super T>映射器) 它返回一个收集器, 该收集器将一个产生int的映射函数应用于每个输入元素, 并返回结果值的摘要统计信息。
公共静态<T>收集器<T, ?, LongSummaryStatistics> summarizingLong(ToLongFunction <?super T>映射器) 它返回一个收集器, 该收集器将长时间生成的映射函数应用于每个输入元素, 并返回结果值的摘要统计信息。
公共静态<T>收集器<T, ?, DoubleSummaryStatistics> summarizingDouble(ToDoubleFunction <?super T>映射器) 它返回一个收集器, 该收集器将双重生成的映射函数应用于每个输入元素, 并返回结果值的摘要统计信息。

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
赞(0)
未经允许不得转载:srcmini » Java收集器

评论 抢沙发

评论前必须登录!