jdk8的新特性(一)
阿新 • • 發佈:2018-12-22
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()); } }