java8新特性Stream之筛选与切片

在文章开始之前,我们创建一个Student类,方便下面操作使用:

public class Student {	
	
	 private String name;
	 private int age;
	 
	public Student(){}
	
	public Student(String name, int age)
	{
		super();
		this.name = name;
		this.age = age;
	}
	
	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}
	public int getAge()
	{
		return age;
	}

	public void setAge(int age)
	{
		this.age = age;
	}
	
	@Override
	public String toString()
	{
		return "Student [name=" + name + ", age=" + age + "]";
	}
}

filter

先看一下 Stream< T > filter(Predicate<? super T> predicate);的源码:

/**
 * Returns a stream consisting of the elements of this stream that match
 * the given predicate.
 *
 * <p>This is an intermediate
 * operation.
 *
 * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>,
 *                  <a href="package-summary.html#Statelessness">stateless</a>
 *                  predicate to apply to each element to determine if it
 *                  should be included
 * @return the new stream
 */
Stream<T> filter(Predicate<? super T> predicate);

其实就是接受lambda,从流中排除某些元素。

比如筛选出年龄大于20岁的学生:

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

class Test{
	public static void main(String []args) {
		 List<Student> stuList = Arrays.asList(new Student("xiaoming",18),
				 							   new Student("xiaoli",22),
				 							   new Student("xiaozhang",19),
				 							   new Student("Tom",26),
				 							   new Student("Kevin",20),
				 							   new Student("Lucy",26)
				 							   );
		 
		 Stream<Student> stream1 = stuList.stream().filter((stu) -> stu.getAge()>20);
		 stream1.forEach(System.out::println);
	}
}

Output:

Student [name=xiaoli, age=22]
Student [name=Tom, age=26]
Student [name=Lucy, age=26]

limit

截断流,使元素不超过limit中传入的数

比如筛选出年龄大于20岁,但不超过2个人数

Stream<Student> stream1 = stuList.stream().filter((stu) -> stu.getAge()>20).limit(2);
stream1.forEach(System.out::println);

output:

Student [name=xiaoli, age=22]
Student [name=Tom, age=26]

找到两个符合条件的就不继续找,有利于效率提高。

skip

跳过元素,返回一个抛弃掉了前n个元素的流。若流中元素不足n个,则返回一个空流

Stream<Student> stream1 = stuList.stream().filter((stu) -> stu.getAge()>20).skip(1);
stream1.forEach(System.out::println);

distinct

去除重复元素,前提是重写hashcode和equals方法

我们新加入了三个重复的元素new Student("Lucy",26),并且在Student类重写了hashcode和equals方法。

class Test{
	public static void main(String []args) {
		 List<Student> stuList = Arrays.asList(new Student("xiaoming",18),
				 							   new Student("xiaoli",22),
				 							   new Student("xiaozhang",19),
				 							   new Student("Tom",26),
				 							   new Student("Kevin",20),
				 							   new Student("Lucy",26),
				 							   new Student("Lucy",26),
				 							   new Student("Lucy",26)
				 							   );
		 
		 Stream<Student> stream1 = stuList.stream().filter((stu) -> stu.getAge()>20).skip(1).distinct();
		 stream1.forEach(System.out::println);
	}
}

Output:

Student [name=Tom, age=26]
Student [name=Lucy, age=26]
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值