package com.gang; import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; public class Test1 { public static void main(String[] args) { Student student1 = new Student("701","张三","da"); Student student2 = new Student("700","李四","da"); Student student3 = new Student("703","王五","da"); Student student4 = new Student("701","赵六","da"); Student student5 = new Student("700","钱七","da"); Student student6 = new Student("701","老八","da"); List<Student> list = Arrays.asList(student1, student2, student3); List<String> collect = list.stream().map(Student::getGrade).collect(Collectors.toList()); List<String> collect1 = list.stream().map((stu) -> {return stu.getGrade();}).collect(Collectors.toList()); System.out.println(collect); System.out.println(collect1); } }
package com.gang; import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; public class Test1 { public static void main(String[] args) { Student student = new Student("dsad1", "dada", "dada"); Student student1 = new Student("dsad2", "dada", "dada"); Student student11= new Student("dsad3", "dada", "dada"); List<Student> list = Arrays.asList(student, student1, student11); //模拟for循环里面的集合 List<Student> list2 = Arrays.asList(student, student1, student11); System.out.println(list2); Map<String, List<Student>> map = list.stream().collect(Collectors.groupingBy(Student::getGrade)); for (Map.Entry<String, List<Student>> entry : map.entrySet()) { //对应分组的id String key = entry.getKey(); //创建分组 利用beanutils.copy复制对应的分组 //这个list就是对应该分组下的人 //相当于外面的小集合等于说把一堆人分片了 List<Student> value = entry.getValue(); //meettype=2的在外面 3的是在里面 少的匹配多的 for (Student student2 : value) { for (Student student3 : list2) { //判断两个人的全局id是否相同如果相同则给里面的数据设置对应的分组id if (student2.getGrade().equals(student3.getGrade())){ //这里配置人员和分组的关系 student3.setClassNumber(key); } } } } System.out.println(list2); } }