// Sections in original order testList.forEach(System.out::println); // Sections sorted by enrollment limit testList.stream() .sorted(Comparator.comparingInt(SectionInfo::getLimit)) .forEachOrdered(System.out::println); // Sum of enrollment limit of all courses System.out.println(testList.stream() .mapToInt(SectionInfo::getLimit) .sum()); // Number of sections in each department testList.stream() .collect( Collectors.groupingBy( SectionInfo::getDepartment, Collectors.counting())) .forEach((s, c) -> System.out.println(s + ": " + c)); // Sum of enrollment limit of all courses by department testList.stream() .collect( Collectors.groupingBy( SectionInfo::getDepartment, Collectors.summingInt(SectionInfo::getLimit))) .forEach((s, c) -> System.out.println(s + ": " + c)); // Sections in original order for section_info in test_list: print(section_info) // Sections sorted by enrollment limit sorted_list = sorted(test_list, key=lambda x: x.limit) print(section_info) for section_info in sorted_list // Sum of enrollment limit of all courses print(sum(section_info.limit for section_info in sorted_list)) // Number of sections in each department print("{0}: {1}".format(key, len(list(group)))) for key, group in groupby( sorted( sorted_list, key=lambda x: x.department), lambda x: x.department) // Sum of enrollment limit of all courses by department print("{0}: {1}".format( key, sum( section_info.limit for section_info in list(group)))) for key, group in groupby( sorted( sorted_list, key=lambda x: x.department), lambda x: x.department)