자바 8 로 필터 링

1240 단어 java8
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]
	}
}

좋은 웹페이지 즐겨찾기