list.stream的流操作

Student student = new Student();
student.setId(10l);
student.setUserId(10L);
student.setAge(10);
student.setName(“班级1”);
student.setClassId(1);

	Student student1 = new Student();
	student1.setId(11l);
	student1.setUserId(111L);
	student1.setAge(11);
	student1.setName("班级1");
	student1.setClassId(1);

	Student student2 = new Student();
	student2.setId(12l);
	student2.setUserId(12L);
	student2.setAge(12);
	student2.setName("班级2");
	student2.setClassId(2);

	Student student3 = new Student();
	student3.setId(12l);
	student3.setUserId(19L);
	student3.setAge(19);
	student3.setName("班级2");
	student3.setClassId(3);
	//获取所有id的集合
	List<Student> students = Arrays.asList(student, student1, student2, student3);
	List<Long> collect = students.stream().map(Student::getId).collect(Collectors.toList());
	System.out.println(collect);

	//获取所有age的k=>v
	Map<Integer, Student> collect1 = students.stream().collect(Collectors.toMap(Student::getAge, a -> a, (v1, v2) -> v2));
	System.out.println(collect1);

	//根据classId分组
	Map<Integer, List<Student>> collect2 = students.stream().collect(Collectors.groupingBy(Student::getClassId));
	System.out.println(collect2);

	//根据年龄排序
	students.stream().sorted((a1,a2)->a1.getAge()-a2.getAge()).collect(Collectors.toList());
	students.sort((Student t1,Student t2)->t1.getAge().compareTo(t2.getAge()));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值