Java8新特性之Stream API詳解
一、前言
StreamAPI在Java8版本中使用,關(guān)注的是對(duì)數(shù)據(jù)的篩選、查找、存儲(chǔ)等
它可以做的事情有:過(guò)濾、排序、映射、歸約
二、使用流程
Stream實(shí)例化中間操作(過(guò)濾、排序、映射、規(guī)約)終止操作(匹配查找、歸約、收集)
三、案例演示
public class EmployeeData { public static List<Employee> getEmployees(){ List<Employee> list = new ArrayList<>(); list.add(new Employee(1001, "馬化騰", 34, 6000.38)); list.add(new Employee(1002, "馬云", 12, 9876.12)); list.add(new Employee(1003, "劉強(qiáng)東", 33, 3000.82)); list.add(new Employee(1004, "雷軍", 26, 7657.37)); list.add(new Employee(1005, "李彥宏", 65, 5555.32)); list.add(new Employee(1006, "比爾蓋茨", 42, 9500.43)); list.add(new Employee(1007, "任正非", 26, 4333.32)); list.add(new Employee(1008, "扎克伯格", 35, 2500.32)); return list; } }
package JDK_8; import org.junit.Test; import java.util.Arrays; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; import java.util.stream.Stream; public class StreamAPI { // 方式1:使用集合創(chuàng)建Stream @Test public void test1() { List<Employee> list = EmployeeData.getEmployees(); // 返回一個(gè)順序流 Stream<Employee> stream = list.stream(); // 返回一個(gè)并行流 Stream<Employee> employeeStream = list.parallelStream(); } // 方式2:使用數(shù)組創(chuàng)建流對(duì)象 @Test public void test2() { Employee e1 = new Employee(1003, "WZY", 26, 3000.69); Employee e2 = new Employee(1007, "王紫玉", 25, 8888); Employee[] list2 = new Employee[]{e1, e2}; Stream<Employee> stream = Arrays.stream(list2); stream.forEach(System.out::println); } // 通過(guò)Stream.of 創(chuàng)建 @Test public void test3() { Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6); stream.forEach(System.out::println); } // Stream.filter進(jìn)行過(guò)濾 @Test public void test4() { List<Employee> list = EmployeeData.getEmployees(); list.stream().filter(employee -> employee.getAge() > 25).forEach(System.out::println); } // 使用StreamAPI篩選 @Test public void test5() { List<Employee> list = EmployeeData.getEmployees(); list.add(new Employee(1004, "wzy", 25, 18888)); list.add(new Employee(1004, "wzy", 25, 18888)); list.add(new Employee(1004, "wzy", 25, 18888)); list.add(new Employee(1004, "wzy", 25, 18888)); list.stream().forEach(System.out::println); System.out.println(); list.stream().distinct().forEach(System.out::println); } @Test public void test6() { List<String> list = Arrays.asList("a", "b", "c"); // map(Function<? super T,? extends R> mapper) // 返回由給定函數(shù)應(yīng)用于此流的元素的結(jié)果組成的流。 list.stream().map(s -> s.toUpperCase()).forEach(System.out::println); } // 使用StreamAPI篩選:工資大于6000的員工 @Test public void test7() { List<Employee> list = EmployeeData.getEmployees(); list.stream().filter(s -> s.getSalary() > 6000).forEach(System.out::println); } // StreamAPI映射:map,接收參數(shù),將參數(shù)轉(zhuǎn)換為其他形式的信息; @Test public void test8() { List<Employee> list = EmployeeData.getEmployees(); list.stream().map(e -> e.getAge()).forEach(System.out::println); } // StreamAPI排序,sorted幫助實(shí)現(xiàn)Comparable接口進(jìn)行對(duì)象比較 @Test public void test9() { List<Employee> list = EmployeeData.getEmployees(); list.stream().sorted((e1, e2) -> { int ageValue = Integer.compare(e1.getAge(), e2.getAge()); if (ageValue != 0) { return ageValue; } else { return Double.compare(e1.getSalary(), e2.getSalary()); } }).forEach(System.out::println); } // StreamAPI匹配:所有元素是否滿(mǎn)足下列條件 @Test public void test10() { List<Employee> list = EmployeeData.getEmployees(); boolean allMatch = list.stream().allMatch(e -> e.getSalary() > 5000); System.out.println(allMatch); } // StreamAPI查找:返回第一個(gè)元素 @Test public void test11() { List<Employee> list = EmployeeData.getEmployees(); Optional<Employee> first = list.stream().findFirst(); System.out.println(first); } // StreamAPI查找:返回薪水?dāng)?shù)最大的員工 @Test public void test12() { List<Employee> list = EmployeeData.getEmployees(); Stream<Double> salaryStream = list.stream().map(e -> e.getSalary()); Optional<Double> max = salaryStream.max(Double::compare); System.out.println(max); } // StreamAPI查找:薪水最小的員工對(duì)像 @Test public void test13() { List<Employee> list = EmployeeData.getEmployees(); Optional<Employee> min = list.stream().min((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary())); System.out.println(min); } // StreamAPI:歸約 @Test public void test14() { List<Employee> list = EmployeeData.getEmployees(); Stream<Double> salaryStream = list.stream().map(e -> e.getSalary()); Optional<Double> sum = salaryStream.reduce((s1, s2) -> s1 + s2); System.out.println(sum.get()); } // StreamAPI:收集 @Test public void test15(){ List<Employee> list = EmployeeData.getEmployees(); List<Employee> employeeList = list.stream().filter(e -> e.getSalary() > 5000).collect(Collectors.toList()); for (Employee employee : employeeList) { System.out.println(employee); } } }
到此這篇關(guān)于Java8新特性之StreamAPI詳解的文章就介紹到這了,更多相關(guān)java StreamAPI詳解內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于通過(guò)Java連接mysql對(duì)反斜杠”\“轉(zhuǎn)義的測(cè)試詳解
這篇文章主要給大家介紹了關(guān)于通過(guò)Java連接mysql對(duì)反斜杠”\“轉(zhuǎn)義的測(cè)試的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家理解反斜杠”\“轉(zhuǎn)義具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-06-06使用maven命令安裝jar包到本地倉(cāng)庫(kù)的方法步驟
這篇文章主要介紹了使用maven命令安裝jar包到本地倉(cāng)庫(kù)的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06Java 實(shí)現(xiàn)FTP服務(wù)實(shí)例詳解
這篇文章主要介紹了Java 實(shí)現(xiàn)FTP服務(wù)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04Java使用策略模式實(shí)現(xiàn)聚石塔接口調(diào)用的問(wèn)題
這篇文章主要介紹了Java使用策略模式實(shí)現(xiàn)聚石塔接口調(diào)用,為了避免多重判斷,而且有更好的擴(kuò)展性,首選了策略模式來(lái)實(shí)現(xiàn),具體解決方法跟隨小編一起看看吧2021-12-12