java8-StreamAPI之collection归约操作
生活随笔
收集整理的這篇文章主要介紹了
java8-StreamAPI之collection归约操作
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一說(shuō)明
經(jīng)過(guò)前一篇的StreamAPI學(xué)習(xí),基本的流操作我相信大家都熟練于心了,那么今天是要詳細(xì)解析一下收集器(collect)這么API
前提要區(qū)分,collect(StreamAPI)與collection(集合),collectors(StreamAPI靜態(tài)工廠是一種歸約操作)是個(gè)不同的東西
二 Collect
初始化信息
public List<Car> InitCar(){
ArrayList<Car> carList = new ArrayList<>();
Car car1 = new Car("100", "black", "中國(guó)", 20);
Car car2 = new Car("101", "gray", "中國(guó)", 30);
Car car3 = new Car("102", "yello", "中國(guó)", 50);
Car car4 = new Car("103", "silvery", "英國(guó)", 20);
Car car5 = new Car("104", "red", "英國(guó)", 30);
carList.add(car1);
carList.add(car2);
carList.add(car3);
carList.add(car4);
carList.add(car5);
return carList;
}
1數(shù)量
@Test
public void countTest(){
List<Car> cars = carFunFactory.InitCar();
// 求數(shù)量
Long count = cars.stream().collect(Collectors.counting());
System.out.println(count);//5
}
2 最大值
@Test
public void maxTest(){
List<Car> cars = carFunFactory.InitCar();
// 求車(chē)價(jià)格最大值的車(chē)
Comparator<Car> carComparator = Comparator.comparingDouble(Car::getPrice);
Optional<Car> maxOptional = cars.stream().collect(Collectors.maxBy(carComparator));
// Car(code=102, color=yello, factory=中國(guó), price=50.0)
System.out.println(maxOptional.get());
}
3 最小值
@Test
public void minTest(){
List<Car> cars = carFunFactory.InitCar();
// 求車(chē)價(jià)格最小值的車(chē)
Comparator<Car> carComparator = Comparator.comparingDouble(Car::getPrice);
Optional<Car> maxOptional = cars.stream().collect(Collectors.minBy(carComparator));
// Car(code=100, color=black, factory=中國(guó), price=20.0)
System.out.println(maxOptional.get());
}
4求和
@Test
public void sumTest(){
List<Car> cars = carFunFactory.InitCar();
// 求所有車(chē)價(jià)格的總和
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();
// 求所有車(chē)價(jià)格的均值
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();
// 求所有車(chē)顏色字符串的拼接
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();
// 求所有車(chē)價(jià)格的總和
Double collect = cars.stream()
.collect(Collectors.reducing(0.0, Car::getPrice, (number, number2) -> number + number2));
System.out.println(collect);//150.0
}
8 分組
根據(jù)車(chē)的制造地分組。分為 中國(guó)和英國(guó)2組
@Test
public void groupingByTest(){
List<Car> cars = carFunFactory.InitCar();
// 根據(jù)車(chē)的制造地分組
Map<String, List<Car>> collect = cars.stream()
.collect(Collectors.groupingBy(Car::getFactory));
//{中國(guó)=[Car(code=100, color=black, factory=中國(guó), price=20.0),
// Car(code=101, color=gray, factory=中國(guó), price=30.0),
// Car(code=102, color=yello, factory=中國(guó), price=50.0)],
// 英國(guó)=[Car(code=103, color=silvery, factory=英國(guó), price=20.0),
// Car(code=104, color=red, factory=英國(guó), price=30.0)]}
System.out.println(collect);
}
9 多級(jí)分組
@Test
public void moreGroupingByTest(){
List<Car> cars = carFunFactory.InitCar();
// 根據(jù)車(chē)的制造地分組,再根據(jù)車(chē)的價(jià)格分組
Map<String, Map<Double, List<Car>>> collect = cars.stream()
.collect(Collectors.groupingBy(Car::getFactory, Collectors.groupingBy(Car::getPrice)));
//{中國(guó)={20.0=[Car(code=100, color=black, factory=中國(guó), price=20.0)],
// 50.0=[Car(code=102, color=yello, factory=中國(guó), price=50.0)],
// 30.0=[Car(code=101, color=gray, factory=中國(guó), price=30.0)]},
// 英國(guó)={20.0=[Car(code=103, color=silvery, factory=英國(guó), price=20.0)],
// 30.0=[Car(code=104, color=red, factory=英國(guó), price=30.0)]}}
System.out.println(collect);
}
10 分區(qū)
分區(qū)是分組里面的一種,只根據(jù)true,false進(jìn)行分組。
@Test
public void groupingByAndCountTest(){
List<Car> cars = carFunFactory.InitCar();
// 根據(jù)車(chē)的價(jià)格是否大于30分區(qū)
Map<Boolean, List<Car>> collect = cars.stream()
.collect(Collectors.partitioningBy(o -> o.getPrice() > 30));
//{false=[Car(code=100, color=black, factory=中國(guó), price=20.0),
// Car(code=101, color=gray, factory=中國(guó), price=30.0),
// Car(code=103, color=silvery, factory=英國(guó), price=20.0),
// Car(code=104, color=red, factory=英國(guó), price=30.0)],
// true=[Car(code=102, color=yello, factory=中國(guó), price=50.0)]}
System.out.println(collect);
}
11 收集為L(zhǎng)ist
@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轉(zhuǎn)為map
@Test
public void toMapTest(){
List<Car> cars = carFunFactory.InitCar();
// 提取新元素key,val轉(zhuǎn)為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();
// 提取 元素轉(zhuǎn)為L(zhǎng)ist
List<String> collect = cars.stream()
.collect(Collectors.mapping(Car::getColor, Collectors.toList()));
// [black, gray, yello, silvery, red]
System.out.println(collect);
}
三 特別說(shuō)明
之前的求和,平均值,最大值等還有一種求法,主要是 double ,int long類(lèi)型。以下示例是double型。
@Test
public void summarizingTest(){
List<Car> cars = carFunFactory.InitCar();
// 求所有車(chē)價(jià)格的總和
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
}
四致謝
這次搜集器講完,打算后面再講解一下并行流,后面就會(huì)進(jìn)入時(shí)間操作,有興趣愛(ài)學(xué)習(xí)的朋友可以關(guān)注我公眾號(hào),支持一下,謝謝。
總結(jié)
以上是生活随笔為你收集整理的java8-StreamAPI之collection归约操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 使用dtaidistance实现dtw算
- 下一篇: Vue2.5开发去哪儿网App 详情页面