概要
源于工作中经常用到stream流处理集合, 记录一下简单用法, 实际工作中可以推导。
代码
1:创建实体类 没有用spring boot 简单的一个实体
private static class BeanVo {
public int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public Integer getFee() {
return fee;
}
public void setFee(Integer fee) {
this.fee = fee;
}
public String goodsName;
public Integer fee;
@Override
public String toString() {
return "BeanVo{" +
"id='" + id + '\'' +
", goodsName='" + goodsName + '\'' +
", fee=" + fee +
'}';
}
}
2:创建几个元素集合
private static List<BeanVo> initList() {
List<BeanVo> beanVos = new ArrayList<>();
BeanVo b1 = new BeanVo();
b1.setId(1);
b1.setGoodsName("商品1");
b1.setFee(9);
beanVos.add(b1);
BeanVo b2 = new BeanVo();
b2.setId(2);
b2.setGoodsName("商品2");
b2.setFee(8);
beanVos.add(b2);
return beanVos;
}
3:实现run方法
public class RunJavaMain {
/**
* JAVA8 stream() 使用案例
*
* @param args
*/
public static void main(String[] args) {
List<BeanVo> beanVos = initList();
sum(beanVos);
filter(beanVos);
extremum(beanVos);
sorted(beanVos);
groupingBy(beanVos);
toMap(beanVos);
}
private static void sum(List<BeanVo> beanVos) {
int sum = beanVos.stream().mapToInt(BeanVo::getFee).sum();
System.out.println("获取对象中某一字段和:" + sum);
int filterSum = beanVos.stream().filter(i -> i.getId() > 1).mapToInt(BeanVo::getFee).sum();
System.out.println("根据过滤字段获取对象中某一字段和:" + filterSum);
}
private static void filter(List<BeanVo> beanVos) {
List<BeanVo> filterField = beanVos.stream().filter(i -> i.getId() > 1).collect(Collectors.toList());
System.out.println("根据字段过滤获取集合");
for (BeanVo beanVo : filterField) {
System.out.println("根据字段过滤获取集合:" + beanVo.toString());
}
}
private static void extremum(List<BeanVo> beanVos) {
Optional<BeanVo> max = beanVos.stream().collect(Collectors.maxBy(Comparator.comparing(BeanVo::getFee)));
System.out.println("根据最大值获取集合中元素:" + max);
Optional<BeanVo> min = beanVos.stream().collect(Collectors.minBy(Comparator.comparing(BeanVo::getFee)));
System.out.println("根据最小值获取集合中元素:" + min);
}
private static void sorted(List<BeanVo> beanVos) {
List<BeanVo> collect = beanVos.stream().sorted(Comparator.comparing(BeanVo::getFee)).collect(Collectors.toList());
System.out.println("按照某个字段进行正序排列");
for (BeanVo beanVo : collect) {
System.out.println("按照某个字段进行正序排列集合:" + beanVo.toString());
}
List<BeanVo> reversedList = beanVos.stream().sorted(Comparator.comparing(BeanVo::getFee).reversed()).collect(Collectors.toList());
System.out.println("按照某个字段进行倒序排列");
for (BeanVo beanVo : reversedList) {
System.out.println("按照某个字段进行倒序排列集合:" + beanVo.toString());
}
}
private static void groupingBy(List<BeanVo> beanVos) {
Map<Integer, List<BeanVo>> collect = beanVos.stream().collect(Collectors.groupingBy(BeanVo::getFee));
collect.forEach((k, v) -> {
System.out.println("按照某个字段进行分组:" + k);
});
}
private static void toMap(List<BeanVo> beanVos) {
System.out.println("按照某个字段进行转化为MAP");
Map<Integer, BeanVo> map = beanVos.stream().collect(Collectors.toMap(BeanVo::getFee, beanVo -> beanVo, (k1, k2) -> k1));
System.out.println("如果 toMap 如果集合对象有重复的key,会报错Duplicate key .... 用 (k1,k2)->k1 来设置,如果有重复的key,则保留key1,舍弃key2");
}
}
小结
在stream中filter过滤器可以做很多事情,和Comparator比较器组合可以处理集合中很大一部分操作。