使用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 lines = Arrays.asList("spring", "node", "mkyong");
		List result1 = lines.stream()
				.filter(line -> !"mkyong".equals(line))
				.collect(Collectors.toList());
		result1.forEach(System.out::println);
		//end:结果spring  node
		//2.过滤list中的数据
		List stuList = new ArrayList();
		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 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]
	}
}

你可能感兴趣的:(java8)