Map<String, List<InputForm>> collect =inputForms.stream() .collect(Collectors.groupingBy(InputForm::getCreateCompanyName));returncollect; } 求最大值,最小值 @GetMapping("/list")publicMap<String, List<InputForm>>list(){ List<InputForm> inputForms =inputFormMapper.selectList(); System.out.prin...
2、 list分组成Map<String,List<String>> 一个班级的学生,按照性别分组,List中只取学生的姓名 Stream<User> userStream = Stream.of(new User(1, "bobo", 2, 0), new User(2, "naitang", 1, 1),new User(3,"guoguo",3,1),new User(4,"dandan",2,0)); Map<Integer, List<String>> dataMa...
importjava.util.Map;importjava.util.stream.Collectors;publicclassMain{publicstaticvoidmain(String[]args){List<Employee>employees=newArrayList<>();// ...(上面的代码)// 使用 Stream API 分组统计Map<String,Long>departmentCount=employees.stream().collect(Collectors.groupingBy(Employee::getDepartment,Collect...
List<User> userList = UserService.getUserList(); //获取用户名称列表 List<String> nameList = userList.stream().map(User::getName).collect(Collectors.toList()); //或者:List<String> nameList = userList.stream().map(user -> user.getName()).collect(Collectors.toList()); //遍历名称列表 ...
这里,Function.identity()表示我们按照字符串本身进行分组,Collectors.counting()用于统计每个分组中的元素个数。 将分组后的结果转换为一个可以排序的集合或列表: 由于我们需要对出现次数进行排序,因此可以将Map中的条目转换为List<Map.Entry<String, Long>>,这样可以方便地进行排序: java List<Map...
*/@TestpublicvoidmapTest(){//获取用户列表List<User>userList=UserService.getUserList();//获取用户名称列表List<String>nameList=userList.stream().map(User::getName).collect(Collectors.toList());//或者:List<String> nameList = userList.stream().map(user -> user.getName()).collect(Collectors...
使用Java 8的新特性可以使用流(Stream)和Lambda表达式来编写一个List中多个重复字符串的统计代码。下面是一个示例代码: importjava.util.ArrayList;importjava.util.List;importjava.util.Map;importjava.util.function.Function;importjava.util.stream.Collectors;publicclassMain{publicstaticvoidmain(String[]args){List...
public static void main(String[] args) { Optional collect1 = students.stream().collect(Collectors.maxBy((s1, s2) -> s1.getAge() - s2.getAge())); Optional collect2 = students.stream().collect(Collectors.minBy((s1, s2) -> s1.getAge() - s2.getAge())); ...
分组统计: @GetMapping("/list") publicvoidlist(){ List<InputForm>inputForms=inputFormMapper.selectList(); System.out.println("inputForms="+inputForms); Map<String,Long>collect=inputForms.stream().collect(Collectors.groupingBy(InputForm::getCreateUserName, Collectors.counting())); System.out.printl...