java8 IntSummaryStatistics Collectors counting summingInt averagingInt maxBy minBy comparingInt

     java8 IntSummaryStatistics Collectors counting summingInt averagingInt maxBy minBy comparingInt

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class TestC {

	public static void main(String[] args) {
		Apple apple1 = new Apple("红富士", 45, "红", "辽宁", LocalDate.of(2019, 10, 12));
		Apple apple2 = new Apple("国光", 60, "绿", "河北", LocalDate.of(2019, 10, 18));
		Apple apple3 = new Apple("红星", 50, "红", "山东", LocalDate.of(2019, 9, 10));
		Apple apple4 = new Apple("黄元帅", 85, "黄", "山东", LocalDate.of(2019, 10, 6));
		Apple apple5 = new Apple("嘎啦", 35, "绿", "山东", LocalDate.of(2019, 8, 21));
		Apple apple6 = new Apple("寒富", 40, "红", "辽宁", LocalDate.of(2019, 9, 20));
		List appleList = new ArrayList<>();
		appleList.add(apple1);
		appleList.add(apple2);
		appleList.add(apple3);
		appleList.add(apple4);
		appleList.add(apple5);
		appleList.add(apple6);
		
		//计数 Collectors.counting()
		long appleCount = appleList.stream().collect(Collectors.counting());
		System.out.println("计数:" + appleCount);
		//求总和 Collectors.summingInt()
		int appleSum = appleList.stream().collect(Collectors.summingInt(Apple::getWeight));
		System.out.println("求总和:" + appleSum);
		//求平均 Collectors.averagingInt()
		double appleAvg = appleList.stream().collect(Collectors.averagingInt(Apple::getWeight));
		System.out.println("求平均:" + appleAvg);
		//求最大
		Optional appleMax = appleList.stream().collect(Collectors.maxBy(Comparator.comparingInt(Apple::getWeight)));
		if (appleMax.isPresent()) {
			System.out.println("求最大:" + appleMax.get());
		}
		//求最小
		Optional appleMin = appleList.stream().collect(Collectors.minBy(Comparator.comparingInt(Apple::getWeight)));
		if (appleMin.isPresent()) {
			System.out.println("求最小:" + appleMin.get());
		}
		//一次求所有
		IntSummaryStatistics all = appleList.stream().collect(Collectors.summarizingInt(Apple::getWeight));
		System.out.println(all.getAverage());
		System.out.println(all.getCount());
		System.out.println(all.getMax());
		System.out.println(all.getMin());
		System.out.println(all.getSum());
	}

}
计数:6
求总和:315
求平均:52.5
求最大:Apple [variety=黄元帅, weight=85, color=黄, placeOfOrigin=山东, productionDate=2019-10-06]
求最小:Apple [variety=嘎啦, weight=35, color=绿, placeOfOrigin=山东, productionDate=2019-08-21]
52.5
6
85
35
315
import java.time.LocalDate;

/**
 * @author InJavaWeTrust
 */
public class Apple {
	
	private String variety;
	private int weight;
	private String color;
	private String placeOfOrigin;
	private LocalDate productionDate;
	
	public Apple() {
	}
	
	public Apple(String variety, int weight, String color, String placeOfOrigin, LocalDate productionDate) {
		this.variety = variety;
		this.weight = weight;
		this.color = color;
		this.placeOfOrigin = placeOfOrigin;
		this.productionDate = productionDate;
	}

	public String getVariety() {
		return variety;
	}
	public void setVariety(String variety) {
		this.variety = variety;
	}
	public int getWeight() {
		return weight;
	}
	public void setWeight(int weight) {
		this.weight = weight;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	public String getPlaceOfOrigin() {
		return placeOfOrigin;
	}
	public void setPlaceOfOrigin(String placeOfOrigin) {
		this.placeOfOrigin = placeOfOrigin;
	}
	public LocalDate getProductionDate() {
		return productionDate;
	}
	public void setProductionDate(LocalDate productionDate) {
		this.productionDate = productionDate;
	}
	
	@Override
	public String toString() {
		return "Apple ["
				+ "variety="        + variety        + ", "
				+ "weight="         + weight         + ", "
				+ "color="          + color          + ", "
				+ "placeOfOrigin="  + placeOfOrigin  + ", "
				+ "productionDate=" + productionDate + 
				"]";
	}
	
}

 

你可能感兴趣的:(java8)