java 对象Student集合Studen中有2个属性name 字符型sex 字符型按sex字段分组并且每100条一组
可以使用Java8的流式编程来实现该功能,具体代码如下:
List<Student> studentList = new ArrayList<>(); // 假设已有Student对象集合
Map<String, List<List<Student>>> groupedStudents =
studentList.stream()
.collect(Collectors.groupingBy(
Student::getSex,
Collectors.partitioningBy(
(student, index) -> index / 100
)
));
上述代码中,groupingBy方法按照Student对象的性别字段进行分组,得到一个Map<String, List<Student>>类型的结果。然后,我们再使用partitioningBy方法将每个分组的List<Student>对象分割成每100条一组的List<List<Student>>对象。最终得到一个Map<String, List<List<Student>>>类型的结果,其中每个键值对表示一个性别分组及其对应的每100条一组的List<Student>对象集合
原文地址: https://www.cveoy.top/t/topic/fgT7 著作权归作者所有。请勿转载和采集!