一說明
經過前一篇的StreamAPI學習,基本的流操作我相信大家都熟練於心了,那么今天是要詳細解析一下收集器(collect)這么API
前提要區分,collect(StreamAPI)與collection(集合),collectors(StreamAPI靜態工廠是一種歸約操作)是個不同的東西
二 Collect
初始化信息
public List<Car> InitCar(){
ArrayList<Car> carList = new ArrayList<>();
Car car1 = new Car("100", "black", "中國", 20);
Car car2 = new Car("101", "gray", "中國", 30);
Car car3 = new Car("102", "yello", "中國", 50);
Car car4 = new Car("103", "silvery", "英國", 20);
Car car5 = new Car("104", "red", "英國", 30);
carList.add(car1);
carList.add(car2);
carList.add(car3);
carList.add(car4);
carList.add(car5);
return carList;
}
1數量
@Test
public void countTest(){
List<Car> cars = carFunFactory.InitCar();
// 求數量
Long count = cars.stream().collect(Collectors.counting());
System.out.println(count);//5
}
2 最大值
@Test
public void maxTest(){
List<Car> cars = carFunFactory.InitCar();
// 求車價格最大值的車
Comparator<Car> carComparator = Comparator.comparingDouble(Car::getPrice);
Optional<Car> maxOptional = cars.stream().collect(Collectors.maxBy(carComparator));
// Car(code=102, color=yello, factory=中國, price=50.0)
System.out.println(maxOptional.get());
}
3 最小值
@Test
public void minTest(){
List<Car> cars = carFunFactory.InitCar();
// 求車價格最小值的車
Comparator<Car> carComparator = Comparator.comparingDouble(Car::getPrice);
Optional<Car> maxOptional = cars.stream().collect(Collectors.minBy(carComparator));
// Car(code=100, color=black, factory=中國, price=20.0)
System.out.println(maxOptional.get());
}
4求和
@Test
public void sumTest(){
List<Car> cars = carFunFactory.InitCar();
// 求所有車價格的總和
Double collect = cars.stream().collect(Collectors.summingDouble(Car::getPrice));
System.out.println(collect);//150.0
}
5求均值
@Test
public void avgTest(){
List<Car> cars = carFunFactory.InitCar();
// 求所有車價格的均值
Double collect = cars.stream().collect(Collectors.averagingDouble(Car::getPrice));
System.out.println(collect);//30.0
}
6字符串連接
@Test
public void joinTest(){
List<Car> cars = carFunFactory.InitCar();
// 求所有車顏色字符串的拼接
String collect = cars.stream().map(Car::getColor).collect(Collectors.joining(","));
System.out.println(collect);//black,gray,yello,silvery,red
}
7 歸約
@Test
public void reduceTest(){
List<Car> cars = carFunFactory.InitCar();
// 求所有車價格的總和
Double collect = cars.stream()
.collect(Collectors.reducing(0.0, Car::getPrice, (number, number2) -> number + number2));
System.out.println(collect);//150.0
}
8 分組
根據車的制造地分組。分為 中國和英國2組
@Test
public void groupingByTest(){
List<Car> cars = carFunFactory.InitCar();
// 根據車的制造地分組
Map<String, List<Car>> collect = cars.stream()
.collect(Collectors.groupingBy(Car::getFactory));
//{中國=[Car(code=100, color=black, factory=中國, price=20.0),
// Car(code=101, color=gray, factory=中國, price=30.0),
// Car(code=102, color=yello, factory=中國, price=50.0)],
// 英國=[Car(code=103, color=silvery, factory=英國, price=20.0),
// Car(code=104, color=red, factory=英國, price=30.0)]}
System.out.println(collect);
}
9 多級分組
@Test
public void moreGroupingByTest(){
List<Car> cars = carFunFactory.InitCar();
// 根據車的制造地分組,再根據車的價格分組
Map<String, Map<Double, List<Car>>> collect = cars.stream()
.collect(Collectors.groupingBy(Car::getFactory, Collectors.groupingBy(Car::getPrice)));
//{中國={20.0=[Car(code=100, color=black, factory=中國, price=20.0)],
// 50.0=[Car(code=102, color=yello, factory=中國, price=50.0)],
// 30.0=[Car(code=101, color=gray, factory=中國, price=30.0)]},
// 英國={20.0=[Car(code=103, color=silvery, factory=英國, price=20.0)],
// 30.0=[Car(code=104, color=red, factory=英國, price=30.0)]}}
System.out.println(collect);
}
10 分區
分區是分組里面的一種,只根據true,false進行分組。
@Test
public void groupingByAndCountTest(){
List<Car> cars = carFunFactory.InitCar();
// 根據車的價格是否大於30分區
Map<Boolean, List<Car>> collect = cars.stream()
.collect(Collectors.partitioningBy(o -> o.getPrice() > 30));
//{false=[Car(code=100, color=black, factory=中國, price=20.0),
// Car(code=101, color=gray, factory=中國, price=30.0),
// Car(code=103, color=silvery, factory=英國, price=20.0),
// Car(code=104, color=red, factory=英國, price=30.0)],
// true=[Car(code=102, color=yello, factory=中國, price=50.0)]}
System.out.println(collect);
}
11 收集為List
@Test
public void toListTest(){
List<Car> cars = carFunFactory.InitCar();
//
List<String> collect = cars.stream()
.map(Car::getColor)
.collect(Collectors.toList());
// [black, gray, yello, silvery, red]
System.out.println(collect);
}
12 收集為set
@Test
public void toSetTest(){
List<Car> cars = carFunFactory.InitCar();
//
Set<Double> collect = cars.stream()
.map(Car::getPrice)
.collect(Collectors.toSet());
// [20.0, 50.0, 30.0]
System.out.println(collect);
}
13 提取key-val轉為map
@Test
public void toMapTest(){
List<Car> cars = carFunFactory.InitCar();
// 提取新元素key,val轉為map
Map<String, Double> collect = cars.stream()
.collect(Collectors.toMap(Car::getColor, Car::getPrice));
// {red=30.0, gray=30.0, black=20.0, yello=50.0, silvery=20.0}
System.out.println(collect);
}
14 提取元素歸約收集
public void mapperTest(){
List<Car> cars = carFunFactory.InitCar();
// 提取 元素轉為List
List<String> collect = cars.stream()
.collect(Collectors.mapping(Car::getColor, Collectors.toList()));
// [black, gray, yello, silvery, red]
System.out.println(collect);
}
三 特別說明
之前的求和,平均值,最大值等還有一種求法,主要是 double ,int long類型。以下示例是double型。
@Test
public void summarizingTest(){
List<Car> cars = carFunFactory.InitCar();
// 求所有車價格的總和
DoubleSummaryStatistics collect = cars.stream().collect(Collectors.summarizingDouble(Car::getPrice));
System.out.println(collect.getSum());//150.0
System.out.println(collect.getAverage());//30.0
System.out.println(collect.getMax());//50.0
System.out.println(collect.getMin());//20.0
System.out.println(collect.getCount());//5
}
四致謝
這次搜集器講完,打算后面再講解一下並行流,后面就會進入時間操作,有興趣愛學習的朋友可以關注我公眾號,支持一下,謝謝。

