常用lamda表达式:常用功能

/**
 * Lamda表达式测试
 * @author xpzhang
 *
 */
public class LamdaTest {
	public static void main(String[] args) {
		//组装数据
		Student a = new Student("zhangsan", "20");
		Student b = new Student("lisi", "18");
		Student c = new Student("wangwu", "22");
		Student d = new Student("lisi", "18");
		Student e = new Student("li7", "18");
		List<Student> list = new ArrayList<>();
		Collections.addAll(list, a,b,c,d,e);
		//取出对象的属性转为list
		List<String> collectList = list.stream().filter(f->Integer.parseInt(f.getAge())>18)
		.map(Student::getUserName).collect(Collectors.toList());
		System.out.println("对象中大于18的姓名转为List:"+collectList);
		//排序
		List<Student> sortedList = list.stream().sorted(Comparator.comparing(Student::getAge)).collect(Collectors.toList());
		System.out.println("按照年龄进行排序:"+sortedList);
		//过滤
		List<Student> filterList = list.stream().filter(fi->Integer.parseInt(fi.getAge())>18).collect(Collectors.toList());
		System.out.println("学生中年纪大于18的:"+filterList);
		//分组
		Map<String, List<Student>> mapList = list.stream().collect(Collectors.groupingBy(Student::getUserName));
		System.out.println("按照对象的名字进行分组:"+mapList+"\n取其中的值:"+mapList.get("li7"));
		//去重(对象属性完全一样)
		List<Student> studentList = list.stream().distinct().collect(Collectors.toList());
		System.out.println("去重后的对象信息:"+studentList);
	}
}

上一篇:半波偶极子天线


下一篇:QT中的延时函数