jdk8的新特性(一)

package com.hgsoft.java8;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

import org.junit.Test;

/*
 * 一、Stream API 的操作步骤:
 * 
 * 1. 创建 Stream
 * 
 * 2. 中间操作
 * 
 * 3. 终止操作(终端操作)
	filter——接收 Lambda , 从流中排除某些元素。
	limit——截断流,使其元素不超过给定数量。
	skip(n) —— 跳过元素,返回一个扔掉了前 n 个元素的流。若流中元素不足 n 个,则返回一个空流。与 limit(n) 互补
	distinct ——去重
	sorted()——自然排序
	sorted(Comparator com)——定制排序
 */
public class TestStreamaAPI {
	
	//1. 创建 Stream
	@Test
	public void test1(){
	Integer[] nums = new Integer[]{1,2,3,4,5};
	Arrays.stream(nums).map((x) -> x*x).forEach(System.out::println);
		
	}
	@Test
	public void test2(){
	Optional<Integer> count = emps.stream().map((e)-> 1).reduce(Integer::sum);
	System.out.println(count.get());
		
	}
	
	//2. 中间操作
	List<Employee> emps = Arrays.asList(
			new Employee(102, "李四", 59, 6666.66),
			new Employee(101, "张三", 18, 9999.99),
			new Employee(103, "王五", 28, 3333.33),
			new Employee(104, "赵六", 8, 7777.77),
			new Employee(104, "赵六", 8, 7777.77),
			new Employee(104, "赵六", 8, 7777.77),
			new Employee(105, "田七", 38, 5555.55)
	);
	
	
	@Test
	public void  test3(){
		System.out.println("----------------------filter筛选------------------------------");
		//filter筛选
		emps.stream().filter(e -> e.getName()!="赵六").forEach(System.out::println);
		
		System.out.println("----------------------sort自然------------------------------");
		//sort自然排序
		emps.stream().map((e)->e.getAge()).sorted().forEach(System.out::println);
		
		System.out.println("----------------------sort自定义------------------------------");
		//自定义排序方式 -x.getAge()+y.getAge()或者通过compare方法
		emps.stream().sorted((x,y)-> -x.getAge()+y.getAge()).forEach(System.out::println);
		
		System.out.println("--------------------------limit--------------------------");
		//截断流,使其元素不超过给定数量
		emps.stream().limit(2).forEach(System.out::println);
		
		System.out.println("---------------------------skip-------------------------");
		//跳过元素,扔掉前n个之后的结果
		emps.stream().skip(3).forEach(System.out::println);
		
		System.out.println("---------------------------distinct-------------------------");
		//去重
		emps.stream().distinct().forEach(System.out::println);
		
		System.out.println("---------------------------allMatch-------------------------");
		boolean b = emps.stream().allMatch(e -> e.getSalary()>5000);
		System.out.println(b);
		
		System.out.println("---------------------------anyMatch-------------------------");
		boolean b1 = emps.stream().anyMatch(e -> e.getSalary()>5000);
		System.out.println(b1);
		
		System.out.println("---------------------------noneMatch-------------------------");
		boolean b2 = emps.stream().noneMatch(e -> e.getSalary()>5000);
		System.out.println(b2);
		

		System.out.println("---------------------------findFirt-------------------------");
//		找到第一个
		Optional<Employee> e1 = emps.parallelStream().findFirst();
		System.out.println(e1);
		
		System.out.println("---------------------------findAny-------------------------");
//		findAny流中能够找到一个即可(parallelStream随机找到一个,stream顺序找到一个)
		Optional<Employee> e2 = emps.parallelStream().findAny();
		System.out.println(e2);
		
		System.out.println("---------------------------count-------------------------");
		long l = emps.stream().count();
		System.out.println(l);
		
		System.out.println("---------------------------max(映射)-------------------------");
		//通过map映射将对象映射到价格
		Optional<Double> em =  emps.stream().map(Employee::getSalary).max(Double::compare);
		System.out.println(em.get());
		
		System.out.println("---------------------------max(比较器)-------------------------");
		//不通过映射,而是传入一个比较器
		Optional<Employee> em1 =  emps.stream().max((x,y)-> Double.compare(x.getSalary(),y.getSalary()));
		System.out.println(em1.get());
		
	}
	
	
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值