forEach的循

someObjects.forEach(obj -> {
   //to do something
})

如果这个循环的目标是找到匹配某个谓词的第一个元素

Optional<SomeObject> result =
    someObjects.stream().filter(obj -> some_condition_met).findFirst();

 

如果你只是想知道集合中是否有一个元素的条件为真,你可以使用anyMatch:

boolean result = someObjects.stream().anyMatch(obj -> some_condition_met);

distinct()  :字符串去重
符 :
/*去重,去除重复对象(每个属性的值都一样的),需要注意的是要先重写对象TestStreamModel的equals和hashCode方法*/
        System.out.println("去重前:" + list);
        List<TestStreamModel> distinctList = list.stream().distinct().collect(Collectors.toList());
        System.out.println("去重后:" + distinctList);

        /*排序,按id升续排列,如果要降续则改成:(a, b) -> b.getId() - a.getId(); a和b都是变量名(可以按自己意愿取名字),都是list中的对象的实例*/
        System.out.println("排序前:" + list);
        List<TestStreamModel> sortList = list.stream().sorted((a, b) -> a.getId() - b.getId()).collect(Collectors.toList());
        System.out.println("排序后" + sortList);
        a.stream().sorted((i, j) -> Integer.valueOf(j.get("sum").toString()) - Integer.valueOf(i.get("sum").toString()))
        a.stream().sorted(Comparator.comparing(StudentInfo::getAge).reversed()).collect(Collectors.toList());
        /*过滤,按照自己的需求来筛选list中的数据,比如我筛选出不及格的(小于60分)的人,t为实例*/
        System.out.println("过滤后:" + list);
        List<TestStreamModel> filterList = list.stream().filter(t -> t.getScore() < 60).collect(Collectors.toList());
        System.out.println("过滤后" + filterList);
        /*map, 提取对象中的某一元素,例子中我取的是每个人的name,注意list中类型对应,如果取的是id或者班级,就应该是integer类型*/
        System.out.println("提取前:" + list);
        List<String> mapList = list.stream().map(t -> t.getName()).collect(Collectors.toList());
        System.out.println("提取后:" + mapList);
        /*统计,统计所有人分数的和, 主要我设置的分数属性是double类型的,所以用mapToDouble,如果是int类型的,则需要用mapToInt*/
        double sum = list.stream().mapToDouble(t -> t.getScore()).sum();
        int count = list.stream().mapToInt(t -> t.getId()).sum();
        /*分组, 按照字段中某个属性将list分组*/
        Map<Integer, List<TestStreamModel>> map = list.stream().collect(Collectors.groupingBy(t -> t.getGrade()));
        System.out.println("按年级分组" + map);
        /*然后再对map处理,这样就方便取出自己要的数据*/
        for (Map.Entry<Integer, List<TestStreamModel>> entry : map.entrySet()) {
            System.out.println("key:" + entry.getKey());
            System.out.println("value:" + entry.getValue());
        }
        /*多重分组,先按年级分组,再按班级分组*/
        Map<Integer/*年级id*/, Map<Integer/*班级id*/, List<TestStreamModel>>> groupMap = list.stream().collect(Collectors.groupingBy(t -> t.getGrade(), Collectors.groupingBy(t -> t.getClasses())));
        System.out.println("按照年级再按班级分组:" + groupMap);
        System.out.println("取出一年级一班的list:" + groupMap.get(1).get(1));
        /*多重分组,一般多重分组后都是为了统计,比如说统计每个年级,每个班的总分数*/
        Map<Integer/*年级id*/, Map<Integer/*班级id*/, Double>> sumMap = list.stream().collect(Collectors.groupingBy(t -> t.getGrade(), Collectors.groupingBy(t -> t.getClasses(), Collectors.summingDouble(t -> t.getScore()))));
        System.out.println(sumMap);
        System.out.println("取出一年级一班的总分:" + sumMap.get(1).get(1));
        /*stream是链式的,这些功能是可以一起使用的,例如:计算每个年级每个班的及格人数*/
        Map<Integer/*年级*/, Map<Integer/*班级*/, Long/*人数*/>> integerMap = list.stream().filter(t -> t.getScore() >= 60).collect(Collectors.groupingBy(t -> t.getGrade(), Collectors.groupingBy(t -> t.getClasses(), Collectors.counting())));
        System.out.println("取出一年级一班及格人数:" + integerMap.get(1).get(1));

        //将list转换map
        Map<String, String> map = list.stream().collect(Collectors.toMap(Person::getId, Person::getName));
        Map<String, Appoint> collect = list.stream().collect(Collectors.toMap(a -> (a.getSchoolId() + "-" + a.getTimeQuantumId() + "-" + a.getMachinPlace()), Function.identity()));
        Map<String, Appoint> collect = list.stream().collect(Collectors.toMap(a -> (a.getSchoolId() + "-" + a.getTimeQuantumId() + "-" + a.getMachinPlace()), a -> a));

        //最大值     
        Map<String, Object> tiyan = a.stream().filter(t -> t.get("tiyan") != null).max(Comparator.comparing(m -> Integer.valueOf(m.get("tiyan").toString()))).get();