1 获取实体类对象中某一个字段转为List
List<Long> userIds = users.stream().map(User::getId).collect(Collectors.toList());
2 根据单个字段字段去除重复
users.stream() .collect( Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(
user - > user.getName()))), ArrayList::new));
3 根据多个字段去除重复
users.stream() .collect( Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(
user ->user.getName()+";"+user.getId()))), ArrayList::new));
4 根据某个字段分组
users.stream().collect(Collectors.groupingBy(User::getName))
5 获取所有的员工id集合
List<Integer> employeeList = employees.stream().map(Employee::getId).collect(toList());
6 获取员工id与员工信息的Map集合
Map<Integer, Employee> idEmployeeMap =employees.stream().collect(
Collectors.toMap(Employee::getId, Function.identity()));
7 员工id与员工姓名的map集合
Map<Integer, String> idNameMap = employees.stream().collect(
Collectors.toMap(Employee:: getId, Employee::getName));
8 部门分组,每个部门id对应多个用户信息
Map<Integer, List<Employee>> departGroupMap = employees.stream().collect(
Collectors.groupingBy(Employee::getDepartId));
9 部门分组,每个部门id对应多个用户名称
Map<Integer, List<String>> departNamesMap =employees.stream().collect( Collectors.groupingBy(
Employee::getDepartId,Collectors.mapping(Employee::getName, Collectors.toList())) );
10 生成到新的集合中
List<OutputRobotTestset.OutputTestset> collect = robotTestsetEntityList.stream().map(
e -> new OutputRobotTestset.OutputTestset(e.getUid(), e.getName())).collect(Collectors.toList());
11 list 和 String 数组互转
-
String 数组转list
String title = "\t 10月上旬\t 10月中旬\t 10月下旬"; String[] arrTitles = title.split("\t"); List list2 = Arrays.stream(arrTitles).collect(Collectors.toList());
-
list 转 String 数组
String[] strs1 = titleList.toArray(new String[titleList.size()]);