Java新特性--Stream的常用方法整理

@Data
@ToString
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
public class Student {

	/**
	 * 学号
	 */
	private Integer id;

	/**
	 * 姓名
	 */
	private String name;

	/**
	 * 年龄
	 */
	private Integer age;

	/**
	 * 所属分组
	 */
	private Integer groupId;

	/**
	 * 得分
	 */
	private Integer score;

	public static List<Student> init() {
		List<Student> students = Lists.newArrayList();
		students.add(new Student().setId(1).setName("张一").setAge(10).setGroupId(1).setScore(10));
		students.add(new Student().setId(2).setName("张二").setAge(10).setGroupId(1).setScore(8));
		students.add(new Student().setId(3).setName("张三").setAge(11).setGroupId(2).setScore(7));
		students.add(new Student().setId(4).setName("张四").setAge(12).setGroupId(2).setScore(9));
		students.add(new Student().setId(5).setName("张五").setAge(15).setGroupId(2).setScore(10));
		students.add(new Student().setId(6).setName("张六").setAge(14).setGroupId(1).setScore(5));
		students.add(new Student().setId(7).setName("张七").setAge(13).setGroupId(1).setScore(4));
		return students;
	}
}
	public static void main(String[] args) {
		List<Student> students = Student.init();

		// 1、生成以id为key,Student为value的Map
		Map<Integer, Student> result1 = students.stream().collect(Collectors.toMap(Student::getId, student -> student));
		System.out.println("result1: " + result1);

		// 2、生成以id为key,name为value的Map
		Map<Integer, String> result2 = students.stream().collect(Collectors.toMap(Student::getId, Student::getName));
		System.out.println("result2: " + result2);

		// 3、以groupId分组,生成key为groupId,value为List<Student>的Map
		Map<Integer, List<Student>> result3 = students.stream().collect(Collectors.groupingBy(Student::getGroupId));
		System.out.println("result3: " + result3);

		// 4、以groupId分组,生成key为groupId,value为List<name>的Map
		Map<Integer, List<String>> result4 = students.stream().collect(Collectors.groupingBy(Student::getGroupId,
				Collectors.mapping(Student::getName, Collectors.toList())));
		System.out.println("result4: " + result4);

		// 5、筛选出id>2且<5.并且 age>11的学生姓名
		List<String> result5 =
				students.stream().filter(student -> student.getId() > 2 && student.getId() < 5 && student.getAge() > 11).map(Student::getName).collect(Collectors.toList());
		System.out.println("result5: " + result5);

		// 6、根据age降序、name升序 Comparator.comparing默认升序 reversed降序
		List<Student> result6 =
				students.stream().sorted(Comparator.comparingInt(Student::getAge).reversed().thenComparing(Student::getName)).collect(Collectors.toList());
		System.out.println("result6: " + result6);

		// 7、截取列表中的第二个学生和第三个学生的姓名
		List<String> result7 = students.stream()
				.skip(1)  // 跳过第一个元素
				.limit(2) // 取接下来的2个元素
				.map(Student::getName).collect(Collectors.toList());
		System.out.println("result7: " + result7);

		// 8、获取age最大
		Student result8 = students.stream().max(Comparator.comparing(Student::getAge)).orElse(null);
		System.out.println("result8: " + result8);

		// 9、获取age最小的学生
		Student result9 = students.stream().min(Comparator.comparing(Student::getAge)).orElse(null);
		System.out.println("result9: " + result9);

		// 10、获取所有学生的总得分
		int result10 = students.stream().mapToInt(Student::getScore).sum();
		System.out.println("result10: " + result10);

		// 11、faltMap处理List<List<Student>>结果
		List<List<Student>> list = new ArrayList<>();
		list.add(students);
		List<String> result11 = list.stream().flatMap(List::stream).map(Student::getName).collect(Collectors.toList());
		System.out.println("result11: " + result11);

		// 12、是否存在一个 student 对象的 age 等于 10:
		boolean result12 = students.stream().anyMatch(student -> student.getAge() == 10);
		System.out.println("result12: " + result12);

		// 13、count 返回流中元素的个数
		long result13 = students.stream().filter(student -> student.getAge() > 12).count();
		System.out.println("result13: " + result13);
	}
result1: {1=Student(id=1, name=张一, age=10, groupId=1, score=10), 2=Student(id=2, name=张二, age=10, groupId=1, score=8), 3=Student(id=3, name=张三, age=11, groupId=2, score=7), 4=Student(id=4, name=张四, age=12, groupId=2, score=9), 5=Student(id=5, name=张五, age=15, groupId=2, score=10), 6=Student(id=6, name=张六, age=14, groupId=1, score=5), 7=Student(id=7, name=张七, age=13, groupId=1, score=4)}
result2: {1=张一, 2=张二, 3=张三, 4=张四, 5=张五, 6=张六, 7=张七}
result3: {1=[Student(id=1, name=张一, age=10, groupId=1, score=10), Student(id=2, name=张二, age=10, groupId=1, score=8), Student(id=6, name=张六, age=14, groupId=1, score=5), Student(id=7, name=张七, age=13, groupId=1, score=4)], 2=[Student(id=3, name=张三, age=11, groupId=2, score=7), Student(id=4, name=张四, age=12, groupId=2, score=9), Student(id=5, name=张五, age=15, groupId=2, score=10)]}
result4: {1=[张一, 张二, 张六, 张七], 2=[张三, 张四, 张五]}
result5: [张四]
result6: [Student(id=5, name=张五, age=15, groupId=2, score=10), Student(id=6, name=张六, age=14, groupId=1, score=5), Student(id=7, name=张七, age=13, groupId=1, score=4), Student(id=4, name=张四, age=12, groupId=2, score=9), Student(id=3, name=张三, age=11, groupId=2, score=7), Student(id=1, name=张一, age=10, groupId=1, score=10), Student(id=2, name=张二, age=10, groupId=1, score=8)]
result7: [张二, 张三]
result8: Student(id=5, name=张五, age=15, groupId=2, score=10)
result9: Student(id=1, name=张一, age=10, groupId=1, score=10)
result10: 53
result11: [张一, 张二, 张三, 张四, 张五, 张六, 张七]
result12: true
result13: 3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值