java8 Stream的基本操作

JAVA8的流式操作
1)1筛选和切片
* filter : 对数据进行过滤
* limit:截断流,使其结果不超过给定的数量
* split ,跳过给定的n个元素,若流中不足n个元素,则返回一个空流 ,与limit(n)互补
* distinct 根据hashCode和equals对流中的元素去重

 public class StreamTest {
	List<Employee> emps = Arrays.asList(new Employee("张三",38,8888.88),
									new Employee("李四",33,6666.66),
									new Employee("王五",40,9999.99),
									new Employee("赵六",25,19999.88),
									new Employee("田七",36,18888.88),
									new Employee("田七",36,18888.88),
									new Employee("田七",36,18888.88)
									);
	/**
	 * 1: 筛选和切片
	 * filter :  对数据进行过滤
	 * limit:截断流,使其结果不超过给定的数量
	 * split ,跳过给定的n个元素,若流中不足n个元素,则返回一个空流 ,与limit(n)互补
	 * distinct 根据hashCode和equals对流中的元素去重
	 */
	@Test
	public void testFilter(){
		emps.stream()
			.filter((emp) -> emp.getAge()>30)
			.forEach(System.out::println);
	}
	
	@Test
	public void testLimt(){
		emps.stream()
			.filter((emp) -> emp.getAge()>35)
			.limit(2)
			.forEach(System.out::println);
	}
	
	@Test
	public void testSkip(){
		emps.stream()
			.filter((emp) -> emp.getAge()>35)
			.skip(2)
			.forEach(System.out::println);
	}
	
	@Test
	public void testDistinct(){
		emps.stream()
			.filter((emp) -> emp.getAge()>35)
			.distinct()
			.forEach(System.out::println);
	}
	
}

猜你喜欢

转载自blog.csdn.net/u012224510/article/details/84770511