使用java 8 进行过滤

package com.demo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;

public class Test5 {

	public static void main(String args[]) {
		//1.过滤list中的基本数据类型
		List<String> lines = Arrays.asList("spring", "node", "mkyong");
		List<String> result1 = lines.stream()
				.filter(line -> !"mkyong".equals(line))
				.collect(Collectors.toList());
		result1.forEach(System.out::println);
		//end:结果spring  node
		//2.过滤list<bean>中的数据
		List<Student> stuList = new ArrayList<Student>();
		Student s1 = new Student("222","zhangsan");
		Student s2 = new Student("222","lisi");
		Student s3 = new Student("212","wangwu");
		stuList.add(s1);
		stuList.add(s2);
		stuList.add(s3);
		List<Student> result2 = stuList.stream()
				//根据两个属性进行过滤
				.filter(s -> StringUtils.equals("222", s.getId()) && StringUtils.endsWith(s.getName(), "san"))
				.collect(Collectors.toList());
		result2.forEach(System.out::println);
		//end:结果:Student [id=222	zhangsan]
	}
}

    原文作者:Nightliar
    原文地址: https://blog.csdn.net/Nightliar/article/details/78653619
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞