Java8新特性之Stream API詳解
更新時間:2021年04月25日 14:30:07 作者:油炸冰可樂
這篇文章主要介紹了Java8新特性之StreamAPI詳解,文中有非常詳細的代碼示例,對正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下
一、前言
StreamAPI在Java8版本中使用,關(guān)注的是對數(shù)據(jù)的篩選、查找、存儲等
它可以做的事情有:過濾、排序、映射、歸約
二、使用流程
Stream實例化中間操作(過濾、排序、映射、規(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, "劉強東", 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();
// 返回一個順序流
Stream<Employee> stream = list.stream();
// 返回一個并行流
Stream<Employee> employeeStream = list.parallelStream();
}
// 方式2:使用數(shù)組創(chuàng)建流對象
@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);
}
// 通過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進行過濾
@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幫助實現(xiàn)Comparable接口進行對象比較
@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匹配:所有元素是否滿足下列條件
@Test
public void test10() {
List<Employee> list = EmployeeData.getEmployees();
boolean allMatch = list.stream().allMatch(e -> e.getSalary() > 5000);
System.out.println(allMatch);
}
// StreamAPI查找:返回第一個元素
@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查找:薪水最小的員工對像
@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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于通過Java連接mysql對反斜杠”\“轉(zhuǎn)義的測試詳解
這篇文章主要給大家介紹了關(guān)于通過Java連接mysql對反斜杠”\“轉(zhuǎn)義的測試的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家理解反斜杠”\“轉(zhuǎn)義具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-06-06
Java使用策略模式實現(xiàn)聚石塔接口調(diào)用的問題
這篇文章主要介紹了Java使用策略模式實現(xiàn)聚石塔接口調(diào)用,為了避免多重判斷,而且有更好的擴展性,首選了策略模式來實現(xiàn),具體解決方法跟隨小編一起看看吧2021-12-12

