stream流常用操作,当作笔记,有更好玩的再加!
package com.azure.stream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;
public class ListCalculate {
public static void main(String[] args) {
List<Money> moneyList = getMoneyList();
// List中的对象属性 求和
int sumInt = moneyList.stream().mapToInt(Money::getAmountInt).sum();
long sumLong = moneyList.stream().mapToLong(Money::getAmountLong).sum();
double sumDouble = moneyList.stream().mapToDouble(Money::getAmountDouble).sum();
BigDecimal sumBigDecimal = moneyList.stream().map(Money::getAmountBigDecimal).reduce(BigDecimal.ZERO, BigDecimal::add);
// List中的对象属性 求最大值
int maxInt = moneyList.stream().mapToInt(Money::getAmountInt).max().getAsInt();
long maxLong = moneyList.stream().mapToLong(Money::getAmountLong).max().getAsLong();
double maxDouble = moneyList.stream().mapToDouble(Money::getAmountDouble).max().getAsDouble();
BigDecimal maxBigDecimal = moneyList.stream().map(Money::getAmountBigDecimal).reduce(BigDecimal.ZERO, BigDecimal::max);
// List中的对象属性 求最小值 注意BigDecimal
int minInt = moneyList.stream().mapToInt(Money::getAmountInt).min().getAsInt();
long minLong = moneyList.stream().mapToLong(Money::getAmountLong).min().getAsLong();
double minDouble = moneyList.stream().mapToDouble(Money::getAmountDouble).min().getAsDouble();
BigDecimal minBigDecimal = moneyList.stream().map(Money::getAmountBigDecimal).reduce(BigDecimal.ZERO, BigDecimal::min);
BigDecimal minBigDecimal2 = moneyList.stream().map(Money::getAmountBigDecimal).min(BigDecimal::compareTo).get();
// List中的对象属性 求平均值
double averageInt = moneyList.stream().mapToInt(Money::getAmountInt).average().getAsDouble();
double averageLong = moneyList.stream().mapToLong(Money::getAmountLong).average().getAsDouble();
double averageDouble = moneyList.stream().mapToDouble(Money::getAmountDouble).average().getAsDouble();
BigDecimal averageBigDecimal = moneyList.stream().map(Money::getAmountBigDecimal).reduce(BigDecimal.ZERO, BigDecimal::add).divide(BigDecimal.valueOf(moneyList.size()), 2, RoundingMode.HALF_UP);
// List中的对象属性 分组
Map<String, List<Money>> map = moneyList.stream().collect(Collectors.groupingBy(Money::getType));
System.out.println("===分组:===");
map.forEach((k, v) -> System.out.println(k));
// List中的对象属性 排序
// 降序-递减
List<Money> paiXuDesc = moneyList.stream().sorted(Comparator.comparing(Money::getAmountInt).reversed()).collect(Collectors.toList());
// 升序-递增
List<Money> paiXuAsc = moneyList.stream().sorted(Comparator.comparing(Money::getAmountInt)).collect(Collectors.toList());
System.out.println("===排序:升序-递增===");
paiXuAsc.forEach(list -> System.out.println(list.getAmountInt()));
// 过滤
System.out.println("===过滤:===");
List<Money> guoLv = moneyList.stream().filter(list -> list.getAmountInt() == 123).collect(Collectors.toList());
guoLv.forEach(list -> System.out.println(list.getType()));
// 去重
System.out.println("===去重:===");
List<Money> quChoNg = moneyList.stream().distinct().collect(Collectors.toList());
quChoNg.forEach(list -> System.out.println(list.getType()));
// 分页
System.out.println("===分页:===");
int page = 2;
int size = 3;
List<Money> fenYe = moneyList.stream().skip((page - 1) * size).limit(size).collect(Collectors.toList());
System.out.println(fenYe);
// 转map:注意这里的key不要重复
System.out.println("===转map:===");
Map<String, Integer> listToMap = moneyList.stream().collect(Collectors.toMap(Money::getType, Money::getAmountInt));
listToMap.forEach((k, v) -> {
System.out.println(k);
System.out.println(v);
});
// 取出某一字段数据
System.out.println("===取出某一字段数据:===");
List<String> typeList = moneyList.stream().map(Money::getType).collect(Collectors.toList());
typeList.forEach(System.out::println);
// 按某个字段分组(map无序,linkedMap有序)
System.out.println("===按某个字段分组:===");
List<List<Money>> newList = new ArrayList<>();
moneyList.stream().collect(Collectors.groupingBy(Money::getType, LinkedHashMap::new, Collectors.toList())).forEach((k, v) -> newList.add(v));
System.out.println(newList);
}
private static List<Money> getMoneyList() {
List<Money> moneyList = new ArrayList<>();
// moneyList.add(new Money("美金", 123, 741L, 369.85, new BigDecimal(321)));
moneyList.add(new Money("美金", 123, 741L, 369.85, new BigDecimal(321)));
moneyList.add(new Money("英镑", 456, 852L, 258.74, new BigDecimal(654)));
// moneyList.add(new Money("英镑", 789, 963L, 147.96, new BigDecimal(987)));
return moneyList;
}
}
class Money {
private String type;
private int amountInt;
private long amountLong;
private double amountDouble;
private BigDecimal amountBigDecimal;
public Money(String type, int amountInt, long amountLong, double amountDouble, BigDecimal amountBigDecimal) {
this.type = type;
this.amountInt = amountInt;
this.amountLong = amountLong;
this.amountDouble = amountDouble;
this.amountBigDecimal = amountBigDecimal;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getAmountInt() {
return amountInt;
}
public void setAmountInt(int amountInt) {
this.amountInt = amountInt;
}
public long getAmountLong() {
return amountLong;
}
public void setAmountLong(long amountLong) {
this.amountLong = amountLong;
}
public double getAmountDouble() {
return amountDouble;
}
public void setAmountDouble(double amountDouble) {
this.amountDouble = amountDouble;
}
public BigDecimal getAmountBigDecimal() {
return amountBigDecimal;
}
public void setAmountBigDecimal(BigDecimal amountBigDecimal) {
this.amountBigDecimal = amountBigDecimal;
}
@Override
public String toString() {
return "Money{" +
"type='" + type + '\'' +
", amountInt=" + amountInt +
", amountLong=" + amountLong +
", amountDouble=" + amountDouble +
", amountBigDecimal=" + amountBigDecimal +
'}';
}
}