Java 8 Stream APIs的使用(终结操作:匹配、查找、归约和收集)

这是最后一篇关于Stream APIs使用的博客,我们将示例Stream对象的终结操作。只有终结操作被调用,前面的操作才会执行;操作结束后,Stream实例将被回收。

终结操作被分为这几类:

  1. 匹配与查找。
  2. 归约
  3. 收集

代码示例:

/**
 * 终结操作:只有当Stream的终结操作被调用,之前的操作才会依次向后进行,这被称作操作延迟。
 * 
 * 1. 匹配与查找。
 * 2. 归约
 * 3. 收集
 * @author michael
 *
 */
public class StreamTest3 {
	private List<Employee> list;

	@BeforeEach
	public void startUp() {
		this.list = EmployeeData.getEmployees();
	}

	/**
	 * 匹配allMatch
	 * 是否所有员工年龄大于18
	 */
	@Test
	public void test1() {
		out.println(list.stream().allMatch(e -> e.getAge() > 18));
		out.println();
	}

	/**
	 * 匹配anyMatch
	 * 是否至少有一个员工年龄大于18
	 */
	@Test
	public void test2() {
		out.println(list.stream().anyMatch(e -> e.getAge() > 18));
		out.println();
	}

	/**
	 * 匹配anyMatch
	 * 是否没有员工姓雷
	 */
	@Test
	public void test3() {
		out.println(list.stream().noneMatch(e -> e.getName().startsWith("雷")));
		out.println();
	}

	/**
	 * 查找findFirst
	 * 返回第一个元素
	 */
	@Test
	public void test4() {
		out.println(list.stream().findFirst().get());
		out.println();
	}

	/**
	 * 查找findAny
	 * 返回任意一个元素
	 */
	@Test
	public void test5() {
		out.println(list.stream().findAny().get());
	}

	/**
	 * 查找count
	 * 返回元素总个数
	 */
	@Test
	public void test6() {
		out.println(list.stream().count());
		out.println();
	}

	/**
	 * 查找max
	 * 定制Comparator返回年龄最大员工
	 */
	@Test
	public void test7() {
		out.println(list.stream().max((e1, e2) -> Integer.compare(e1.getAge(), e2.getAge())).get());
		out.println();
	}

	/**
	 * 查找min
	 * 定制Comparator返回工资最低员工
	 */
	@Test
	public void test8() {
		out.println(list.stream().min((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary())).get());
		out.println();
	}

	/**
	 * 查找forEach
	 * 返回所有员工姓名
	 */
	@Test
	public void test9() {
		list.stream().forEach(e -> {out.println(e.getName());});
		out.println();
	}

	/**
	 * 归约reduce
	 * 员工资求和
	 */
	@Test
	public void test10() {
		//先map后reduce
		//out.println(list.stream().map(Employee :: getSalary).reduce((s1, s2) -> s1 + s2).get());
		out.println(list.stream().map(Employee :: getSalary).reduce(Double :: sum).get());
		out.println();
	}

	/**
	 * 收集collector
	 * 返回工资大于6000员工的集合
	 */
	@Test
	public void test11() {

		//返回list集合
		List<Employee> list = this.list.stream().filter(e -> e.getSalary() > 6000).collect(Collectors.toList());
		list.forEach(e -> out.println(e.getName()));
		out.println();
		
		//返回set集合
		Set<Employee> set = this.list.stream().filter(e -> e.getSalary() > 6000).collect(Collectors.toSet());
		set.forEach(e -> out.println(e.getName()));
	}
}

输出:

test10
48424.08

test11
马化腾
马云
雷军
比尔盖茨

马化腾
马云
比尔盖茨
雷军
test1
false

test2
true

test3
false

test4
Employee{id=1001, name='马化腾', age=34, salary=6000.38}

test5
Employee{id=1001, name='马化腾', age=34, salary=6000.38}

test6
8

test7
Employee{id=1005, name='李彦宏', age=65, salary=5555.32}

test8
Employee{id=1008, name='扎克伯格', age=35, salary=2500.32}

test9
马化腾
马云
刘强东
雷军
李彦宏
比尔盖茨
任正非
扎克伯格
发布了70 篇原创文章 · 获赞 4 · 访问量 3034

猜你喜欢

转载自blog.csdn.net/qq_34515959/article/details/105040427