1、List中根据对象某一个字段进行求和
第一种方式
int suma = list.stream().map(e -> e.getAge()).reduce(Integer::sum).get();//求和
System.out.println(suma);
int maxa = list.stream().map(e -> e.getAge()).reduce(Integer::max).get();//最大
System.out.println(maxa);
int mina = list.stream().map(e -> e.getAge()).reduce(Integer::min).get();//最小
System.out.println(mina);
第二种方式
double doublesum = list.stream().mapToDouble(Student::getAge).sum();//和
System.out.println(doublesum);
int intmax = list.stream().mapToInt(Student::getAge).max().getAsInt();//最大
System.out.println(intmax);
int intmin = list.stream().mapToInt(Student::getAge).min().getAsInt();//最小
System.out.println(intmin);
double avg = list.stream().mapToDouble(Student::getAge).average().getAsDouble();//平均
System.out.println(avg);
2、对list进行分页处理
List<PlantinfoVo> subList=new ArrayList<>();
subList = plantinfoVoList.stream().skip((page.getPageNum() - 1) * page.getPageSize()).limit(page.getPageSize()).
collect(Collectors.toList());
3、根据需要留下来的条件过滤出list集合,下面list中需要获得devicetypeName=m或者p或者w的list集合
List<Device> relationModels = deviceMapper.selectDevice(id);
List<Device> device = relationModels.stream().filter(x -> x.getDeviceTypeName().equals("w") || x.getDeviceTypeName().equals("m") || x.getDeviceTypeName().equals("p")).collect(Collectors.toList());
4、根据list中对象某一字段的大小进行排序
List<Person2> personList = new ArrayList<Person2>();
personList.add(new Person2("Tom", 8900, 18,"male", "A"));
personList.add(new Person2("Jack", 7000, 19,"male", "A"));
personList.add(new Person2("Lily", 7800, 20,"female", "D"));
personList.add(new Person2("Anni", 8200, 18,"female", "B"));
personList.add(new Person2("Owen", 9500, 23,"male", "D"));
personList.add(new Person2("Alisa", 7900, 28,"female", "C"));
//根据List集合对象的sort方法(语句后x.get与y.get调换位置即为倒序)
personList.sort((x,y) -> Integer.compare(x.getSalary(),y.getSalary()));//int类型正序
personList.forEach(System.out::println);
System.out.println("================================");
personList.sort((a, b) -> a.getSalary() - b.getSalary());//int类型正序
personList.forEach(System.out::println);
System.out.println("================================");
personList.sort(Comparator.comparing(Person2::getSalary));//int类型正序排序
personList.forEach(System.out::println);
System.out.println("================================");
personList.sort((a,b) -> a.getLevel().compareTo(b.getLevel()));//其他类型正序
personList.forEach(System.out::println);
System.out.println("================================");
//stream流的sorted方法正序排序(倒序:Comparator.comparing(Person2::getSalary).reversed())
personList = personList.stream().sorted(Comparator.comparing(Person2::getSalary)).collect(Collectors.toList());
personList.forEach(System.out::println);
System.out.println("================================");