使用java 8 提取出list中bean的某一属性

package com.demo;
 
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
 
public class Test6 {
 
	public static void main(String[] args) {
		List<Student> stuList = new ArrayList<Student>();
		Student st1 = new Student("123","aaa");
		Student st2 = new Student("234","bbb");
		Student st3 = new Student("345","ccc");
		Student st4 = new Student("345","ccc");
		stuList.add(st1);
		stuList.add(st2);
		stuList.add(st3);
		stuList.add(st4);
		//1.提取出list对象中的一个属性
		List<String> stIdList1 = stuList.stream()
				.map(Student::getId)
				.collect(Collectors.toList());
		stIdList1.forEach(s -> System.out.print(s+" "));
		System.out.println();
		System.out.println("----------");
		
		//2.提取出list对象中的一个属性并去重
		List<String> stIdList2 = stuList.stream()
				.map(Student::getId).distinct()
				.collect(Collectors.toList());
		stIdList2.forEach(s -> System.out.print(s+" "));
		/*	结果:
			123 234 345 345  
			----------
			123 234 345 
		*/
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值