List<ExerciseLevelIndicators> list = exerciseLevelIndicatorsRepository.getSpiderChartAndLineChart(name);
//根据某个属性分组
Map<Long, List<ExerciseLevelIndicators>> collect = list.stream()
.collect(Collectors.groupingBy(ExerciseLevelIndicators::getCommandActualScore));
System.out.println(collect);
System.out.println();
System.out.println();
//根据某个属性分组 ,汇总某个属性
Map<String, LongSummaryStatistics> collect1 = list.stream().collect(Collectors.groupingBy(ExerciseLevelIndicators::getArmedUnitName,
Collectors.summarizingLong(ExerciseLevelIndicators::getCommandActualScore)));
System.out.println(collect1);
System.out.println();
System.out.println();
//根据某个属性添加条件过滤数据
list = list.stream().filter(o -> !o.getArmedUnitName().equals("A营")).collect(Collectors.toList());
System.out.println(list);
System.out.println();
System.out.println();
//判断一组对象里面有没有属性值是某个值
boolean b = list.stream().anyMatch(m -> "A营".equals(m.getArmedUnitName()));
System.out.println(b);
System.out.println();
System.out.println();
//取出一组对象的某个属性组成一个新集合
List<String> collect2 = list.stream().map(ExerciseLevelIndicators::getArmedUnitName).collect(Collectors.toList());
System.out.println(collect2);
和 最大 最小 平均数
public class HelloWorld {
private static final DecimalFormat df = new DecimalFormat("0.00");//保留两位小数点
public static void main(String[] args) {
Random random = new Random();
List<User> list = new ArrayList<>();
for(int i=1;i<=5;i++) {
double weight = random.nextDouble() * 100 + 100;//随机身高:100-200
User u = new User(i, "用户-" + i, weight);
list.add(u);
}
System.out.println("用户:" + list);
double sum = list.stream().mapToDouble(User::getHeight).sum();
System.out.println("身高 总和:" + df.format(sum));
double max = list.stream().mapToDouble(User::getHeight).max().getAsDouble();
System.out.println("身高 最大:" + df.format(max));
double min = list.stream().mapToDouble(User::getHeight).min().getAsDouble();
System.out.println("身高 最小:" + df.format(min));
double average = list.stream().mapToDouble(User::getHeight).average().getAsDouble();
System.out.println("身高 平均:" + df.format(average));
list.sort(Comparator.comparing(对象::字段).reversed())//返回一个强制此比较器反向排序的比较器。
}
private static class User{
Integer id;
String name;
double height;//身高
public User(Integer id, String name, double height) {
this.id = id;
this.name = name;
this.height = height;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", height=" + height +
'}';
}
}
}