入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

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

创建时间:2017-11-28 投稿人: 浏览次数:1426
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 
		*/
	}
}

声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
  • 上一篇:没有了
  • 下一篇:没有了
未上传头像