目录
- 过滤filter()
- 获取最大最小值
- 根据条件统计数量
- list转map
- key值唯一
- key值不唯一
- distinct去重
- groupingBy分组
- map遍历
- 取list中某元素组成新的list
- list转数组
- String
- 基本数据类型数组转换
- 数组转list
- Arrays.asList()
- Collections.addAll
- 基本数据类型数组转list
- 源码和执行结果参考
- 执行源码
- 执行结果
过滤filter()
List<DemoEntity> filterList = initDatas.stream().filter(demo -> (StringUtils.isNotEmpty(demo.getBrief()))).collect(Collectors.toList());
// 循环
filterList.forEach(demo->System.out.println(demo.toString()));
获取最大最小值
List<DemoEntity> filterAgeList = initDatas.stream().filter(demo -> demo.getAge() != null).collect(Collectors.toList());
Integer maxAge = filterAgeList.stream().max(Comparator.comparing(DemoEntity::getAge)).get().getAge();
Integer minAge = filterAgeList.stream().min(Comparator.comparing(DemoEntity::getAge)).get().getAge();
根据条件统计数量
long count = initDatas.stream().distinct().filter(DemoEntity -> DemoEntity.getAge() != null && DemoEntity.getAge() > 30).count();
System.out.println("去重后统计大于30岁的人数="+count);
list转map
key值唯一
Map<Integer, DemoEntity> initMap = initDatas.stream().collect(Collectors.toMap(DemoEntity::getId, demo -> demo));
key值不唯一
- key值冲突会报错,可以设置重复覆盖操作, 后面数据覆盖前面的指定key2,指定key1则前面的覆盖后续重复的key的值
Exception in thread "main" java.lang.IllegalStateException: Duplicate key DemoEntity(id=6, name=Monica, sex=F, age=59, brief=球花, birthDate=Wed Sep 30 00:00:00 CST 1964)
at java.util.stream.Collectors.lambda$throwingMerger$0(Collectors.java:133)
at java.util.HashMap.merge(HashMap.java:1254)
at java.util.stream.Collectors.lambda$toMap$58(Collectors.java:1320)
at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1380)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at com.renxiaozhao.service.demo.stream.StreamUtilDemo.main(StreamUtilDemo.java:32)
Map<Integer, DemoEntity> initMap = initDatas.stream().collect(Collectors.toMap(DemoEntity::getId, Function.identity(), (key1, key2) -> key2));
distinct去重
List<DemoEntity> distincts = initDatas.stream().distinct().collect(Collectors.toList());
groupingBy分组
Map<Integer, List<DemoEntity>> groupMap = distincts.stream().collect(Collectors.groupingBy(demo -> demo.getId()));
map遍历
groupMap.forEach((key, value) -> {
System.out.println("key = " + key + ", value = " + value);
});
取list中某元素组成新的list
List<String> names = initDatas.stream().map(DemoEntity::getName).collect(Collectors.toList());
List<Integer> ids = initDatas.stream().map(DemoEntity::getId).collect(Collectors.toList());
list转数组
String
String[] arr = names.toArray(new String[names.size()]);
基本数据类型数组转换
int[] intArr = ids.stream().mapToInt(Integer::intValue).toArray();
数组转list
Arrays.asList()
String[] arr = {"aa","bb","cc"};
List<String> list = Arrays.asList(arr);
list不能增删改操作
Collections.addAll
String[] arr = {"aa","bb","cc"};
List<String> list1= new ArrayList<>();
Collections.addAll(list1, arr);
list可以增删改操作
基本数据类型数组转list
int[] intArr= {1,2,3};
List<Integer> intList = Arrays.stream(intArr).mapToObj(Integer::new).collect(Collectors.toList());
源码和执行结果参考
执行源码
package com.renxiaozhao.service.demo.stream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
public class StreamUtilDemo {
public static void main(String[] args) throws ParseException {
//初始化数据
List<DemoEntity> initDatas = initData();
//获取去除简介brief为空的数据后的list
List<DemoEntity> filterList = initDatas.stream().filter(demo -> (StringUtils.isNotEmpty(demo.getBrief()))).collect(Collectors.toList());
System.out.println("过滤brief为空后的数据-----------------------------------");
filterList.forEach(demo->System.out.println(demo.toString()));
//获取最成熟的年龄,先过滤年龄为空的数据
List<DemoEntity> filterAgeList = initDatas.stream().filter(demo -> demo.getAge() != null).collect(Collectors.toList());
Integer maxAge = filterAgeList.stream().max(Comparator.comparing(DemoEntity::getAge)).get().getAge();
Integer minAge = filterAgeList.stream().min(Comparator.comparing(DemoEntity::getAge)).get().getAge();
System.out.println("最大年龄="+maxAge+",最小年龄="+minAge);
//获取去重后大于30岁的人数
long count = initDatas.stream().distinct().filter(DemoEntity -> DemoEntity.getAge() != null && DemoEntity.getAge() > 30).count();
System.out.println("去重后统计大于30岁的人数="+count);
//list转map key值冲突会报错,可以设置重复覆盖操作
//Map<Integer, DemoEntity> initMap = initDatas.stream().collect(Collectors.toMap(DemoEntity::getId, demo -> demo));
//后面数据覆盖前面的指定key2,指定key1则前面的覆盖后续重复的key的值
System.out.println("list转map key冲突时覆盖前面的key值设置,详细数据如下:");
Map<Integer, DemoEntity> initMap = initDatas.stream().collect(Collectors.toMap(DemoEntity::getId, Function.identity(), (key1, key2) -> key2));
initMap.forEach((key, value) -> {
System.out.println("key = " + key + ", value = " + value);
});
//分组
System.out.println("distinct去重");
List<DemoEntity> distincts = initDatas.stream().distinct().collect(Collectors.toList());
System.out.println("list转Map,按照id分组");
Map<Integer, List<DemoEntity>> groupMap = distincts.stream().collect(Collectors.groupingBy(demo -> demo.getId()));
groupMap.forEach((key, value) -> {
System.out.println("key = " + key + ", value = " + value);
});
//取list中name组成新的list
List<String> names = initDatas.stream().map(DemoEntity::getName).collect(Collectors.toList());
List<Integer> ids = initDatas.stream().map(DemoEntity::getId).collect(Collectors.toList());
System.out.println("新list:"+names.toString());
System.out.println("新list去重:"+names.stream().distinct().collect(Collectors.toList()).toString());
//list转数组
String[] arr = names.toArray(new String[names.size()]);
System.out.println("list.toarray转数组"+Arrays.toString(arr));
int[] intArr = ids.stream().mapToInt(Integer::intValue).toArray();
System.out.println("list通过stream转数组"+Arrays.toString(intArr));
//数组转list
List<String> list = Arrays.asList(arr);
System.out.println("Arrays.asList转List,只运行查看操作"+list.toString());
List<String> list1 = new ArrayList<>();
Collections.addAll(list1, arr);
System.out.println("Collections.addAll,通过new一个List,可以进行增删改操作"+list1.toString());
//基本数据数组转list
List<Integer> intList = Arrays.stream(intArr).mapToObj(Integer::new).collect(Collectors.toList());
System.out.println("mapToObj基本数据数组转list"+intList.toString());
}
private static List<DemoEntity> initData() throws ParseException{
List<DemoEntity> initDatas = new ArrayList<>();
for (int i = 0;i < 5;i++) {
DemoEntity demo = new DemoEntity();
demo.setId(i+1);
demo.setName("TOM_" + i);
demo.setBirthDate(new Date());
demo.setAge(5*(i+1));
demo.setSex('M');
initDatas.add(demo);
}
//重复数据
String dateString = "1964-09-30";
Date date= new SimpleDateFormat("yyyy-MM-dd").parse(dateString);
DemoEntity demoF1 = new DemoEntity();
demoF1.setId(6);
demoF1.setName("Monica");
demoF1.setBirthDate(date);
demoF1.setBrief("球花");
demoF1.setAge(59);
demoF1.setSex('F');
initDatas.add(demoF1);
DemoEntity demoF2 = new DemoEntity();
demoF2.setId(6);
demoF2.setName("Monica");
demoF2.setBirthDate(date);
demoF2.setBrief("球花");
demoF2.setAge(59);
demoF2.setSex('F');
initDatas.add(demoF2);
//年龄为空
DemoEntity demoF3 = new DemoEntity();
demoF3.setId(6);
demoF3.setName("Monica");
demoF3.setBirthDate(date);
demoF3.setBrief("球花");
demoF3.setSex('F');
initDatas.add(demoF3);
System.out.println("初始数据-----------------------------------");
initDatas.forEach(demo->System.out.println(demo.toString()));
return initDatas;
}
}
package com.renxiaozhao.service.demo.stream;
import java.util.Date;
import org.springframework.format.annotation.DateTimeFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
public class DemoEntity {
@ApiModelProperty(value = "主键")
private Integer id;
@ApiModelProperty(value = "姓名")
private String name;
@ApiModelProperty(value = "性别:男-M 女-F")
private char sex;
@ApiModelProperty(value = "年龄")
private Integer age;
@ApiModelProperty(value = "简介")
private String brief;
@ApiModelProperty(value = "生日")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date birthDate;
}
执行结果
初始数据-----------------------------------
DemoEntity(id=1, name=TOM_0, sex=M, age=5, brief=null, birthDate=Fri Jul 07 13:23:24 CST 2023)
DemoEntity(id=2, name=TOM_1, sex=M, age=10, brief=null, birthDate=Fri Jul 07 13:23:24 CST 2023)
DemoEntity(id=3, name=TOM_2, sex=M, age=15, brief=null, birthDate=Fri Jul 07 13:23:24 CST 2023)
DemoEntity(id=4, name=TOM_3, sex=M, age=20, brief=null, birthDate=Fri Jul 07 13:23:24 CST 2023)
DemoEntity(id=5, name=TOM_4, sex=M, age=25, brief=null, birthDate=Fri Jul 07 13:23:24 CST 2023)
DemoEntity(id=6, name=Monica, sex=F, age=59, brief=球花, birthDate=Wed Sep 30 00:00:00 CST 1964)
DemoEntity(id=6, name=Monica, sex=F, age=59, brief=球花, birthDate=Wed Sep 30 00:00:00 CST 1964)
DemoEntity(id=6, name=Monica, sex=F, age=null, brief=球花, birthDate=Wed Sep 30 00:00:00 CST 1964)
过滤brief为空后的数据-----------------------------------
DemoEntity(id=6, name=Monica, sex=F, age=59, brief=球花, birthDate=Wed Sep 30 00:00:00 CST 1964)
DemoEntity(id=6, name=Monica, sex=F, age=59, brief=球花, birthDate=Wed Sep 30 00:00:00 CST 1964)
DemoEntity(id=6, name=Monica, sex=F, age=null, brief=球花, birthDate=Wed Sep 30 00:00:00 CST 1964)
最大年龄=59,最小年龄=5
去重后统计大于30岁的人数=1
list转map key冲突时覆盖前面的key值设置,详细数据如下:
key = 1, value = DemoEntity(id=1, name=TOM_0, sex=M, age=5, brief=null, birthDate=Fri Jul 07 13:23:24 CST 2023)
key = 2, value = DemoEntity(id=2, name=TOM_1, sex=M, age=10, brief=null, birthDate=Fri Jul 07 13:23:24 CST 2023)
key = 3, value = DemoEntity(id=3, name=TOM_2, sex=M, age=15, brief=null, birthDate=Fri Jul 07 13:23:24 CST 2023)
key = 4, value = DemoEntity(id=4, name=TOM_3, sex=M, age=20, brief=null, birthDate=Fri Jul 07 13:23:24 CST 2023)
key = 5, value = DemoEntity(id=5, name=TOM_4, sex=M, age=25, brief=null, birthDate=Fri Jul 07 13:23:24 CST 2023)
key = 6, value = DemoEntity(id=6, name=Monica, sex=F, age=null, brief=球花, birthDate=Wed Sep 30 00:00:00 CST 1964)
distinct去重
list转Map,按照id分组
key = 1, value = [DemoEntity(id=1, name=TOM_0, sex=M, age=5, brief=null, birthDate=Fri Jul 07 13:23:24 CST 2023)]
key = 2, value = [DemoEntity(id=2, name=TOM_1, sex=M, age=10, brief=null, birthDate=Fri Jul 07 13:23:24 CST 2023)]
key = 3, value = [DemoEntity(id=3, name=TOM_2, sex=M, age=15, brief=null, birthDate=Fri Jul 07 13:23:24 CST 2023)]
key = 4, value = [DemoEntity(id=4, name=TOM_3, sex=M, age=20, brief=null, birthDate=Fri Jul 07 13:23:24 CST 2023)]
key = 5, value = [DemoEntity(id=5, name=TOM_4, sex=M, age=25, brief=null, birthDate=Fri Jul 07 13:23:24 CST 2023)]
key = 6, value = [DemoEntity(id=6, name=Monica, sex=F, age=59, brief=球花, birthDate=Wed Sep 30 00:00:00 CST 1964), DemoEntity(id=6, name=Monica, sex=F, age=null, brief=球花, birthDate=Wed Sep 30 00:00:00 CST 1964)]
新list:[TOM_0, TOM_1, TOM_2, TOM_3, TOM_4, Monica, Monica, Monica]
新list去重:[TOM_0, TOM_1, TOM_2, TOM_3, TOM_4, Monica]
list.toarray转数组[TOM_0, TOM_1, TOM_2, TOM_3, TOM_4, Monica, Monica, Monica]
list通过stream转数组[1, 2, 3, 4, 5, 6, 6, 6]
Arrays.asList转List,只运行查看操作[TOM_0, TOM_1, TOM_2, TOM_3, TOM_4, Monica, Monica, Monica]
Collections.addAll,通过new一个List,可以进行增删改操作[TOM_0, TOM_1, TOM_2, TOM_3, TOM_4, Monica, Monica, Monica]
mapToObj基本数据数组转list[1, 2, 3, 4, 5, 6, 6, 6]