以前想对数据进行分组统计之类的操做,须要使用关系型数据库的group by等操做来实现。但这两天工做中碰到一个问题,数据从其余系统获取而再也不是我从数据库中取了,也就是说我无法使用关系型数据库提供的操做方法了。了解了一下java8中stream 对数据的处理。java
此次碰到的需求是,拿到经过dubbo接口调用其余系统返回的数据,而后对这些进行一个相似于聚合的操做,把多个属性相同的对象视为同一个对象。数据库
假设有一个车辆类型this
@Data
public class Car {
private String name;
private int id;
private String color;
public Car(String name, int id, String color) {
this.name = name;
this.id = id;
this.color = color;
}
//constructors, getter/setters
}
List cars = Arrays.asList(
new Car("golf", 1, "red"),
new Car("bmw", 2, "red"),
new Car("mini", 3, "black"),
new Car("ds", 4, "white"),
new Car("bmw", 5, "gold"),
new Car("golf", 6, "red"),
new Car("bmw", 7, "gold"),
new Car("ds", 8,"white")
);
若是想根据名称来分类,而且统计各品牌数量code
Map sum = cars.stream().collect(
Collectors.groupingBy(Car::getName, Collectors.counting()));
System.out.println(sum);
输出结果是:{golf=2, mini=1, bmw=3, ds=2}对象
若是想根据多个字段来分类,能够再另外建立一个封装类型,把要分类的字段放进去,Car类再添加一个方法来获取这个类的对象。接口
@Data
public class Count {
private String name;
private String color;
public Count(String name, String color) {
this.name = name;
this.color = color;
}
}
修改一下Car类get
@Data
public class Car {
private String name;
private int id;
private String color;
public Car(String name, int id, String color) {
this.name = name;
this.id = id;
this.color = color;
}
public Count getCount() {
return new Count(name, color);
}
//constructors, getter/setters
}
Map counting = cars.stream().collect(
Collectors.groupingBy(Car::getCount, Collectors.counting()));
System.out.println(counting);
输出结果:{Count(name=bmw, color=gold)=2, Count(name=mini, color=black)=1, Count(name=ds, color=white)=2, Count(name=bmw, color=red)=1, Count(name=golf, color=red)=2}it