List<String> idList=().map(Order::getId()).collect(()); (idList)
结果 输出第一个: ["MCS-2019-1123", "MCS-2019-1124", "MCS-2019-1125"] [1, 2, 3]
1、遍历-foreach
().forEach(student->{ //处理逻辑,打印出所有学生的姓名 (()); });
2、筛选list
filter函数的()里,应该放逻辑,判断条件,将符合条件的放到resultList中 代码如下,筛选集合中所有性别为女的学生 List<Student> resultList = ().filter(student -> ((),"女")).collect(()); ().forEach(student->{ (()); });
3、list去重
根据性别去重 List<Student> unique = ().collect(( (() -> new TreeSet<>((Student::getSex))), ArrayList::new)); ().forEach(student->{ (()); });
4、取出list集合对象中某一个属性
取出每个对象中的姓名组成一个新的集合 List<String> listStr = ().map(Student::getName).collect(()); 去重 List<String> listNew = listStr .stream().map(Student::getName).distinct().collect(());
5、list与map互转,并根据某一属性进行分组
list转map (下方studentMap运行会报错,因为作为key值,name不能重复,所以正式开发中应该使用唯一性id作为key值) Map<String, Student> studentMap = ().collect((Student::getName, student -> student)); list转数组 String[] listStrs = () .filter(e -> ((), "男")) .sorted((Student::getName)) .map(Student::getName).toArray(String[]::new); list转map并且分组 Map<String, List<Student>> listMap = ().collect((Student::getSex))
根据对象某些属性,进行分组 Map<List, List> studentsMap= () .collect((f -> (),()))); map转list List<Student> collect = ().stream().collect(());
6、过滤属性为空的字段
Student s6 = new Student("",30,"男"); (s6); List<String> stringList = ().map(s -> ()).filter(s -> !()).collect(());
7、根据某一属性进行计算
根据年龄求最大值、最小值、平均值、总和、个数 IntSummaryStatistics resultNum = ().mapToInt((item)->()).summaryStatistics(); ("max:"+()); ("min:"+()); ("sum:"+()); ("average:"+()); ("count:"+());
1、求和有三种类型,mapToInt,mapToLong,mapToDouble 2、如果是Bigdecimal数值类型,则计算方法如下,(新建对象) Frult frult1 = new Frult("西瓜",new BigDecimal(1)); Frult frult2 = new Frult("梨子",new BigDecimal(2)); List<Frult> frultList = new ArrayList<>(); (frult1); (frult2); BigDecimal totalPrice = ().map(Frult::getPrice).reduce(, BigDecimal::add); //或者用mapToInt()进行强转(int->Bigdecimal)