- Lambda運算式
- 函式式介面
- *方法參考和構造器呼叫
- Stream API
- 介面中的默認方法和靜態方法
- 新時間日期API
Lambda運算式
定義過濾方法:
public List<Product> filterProductByPredicate(List<Product> list,MyPredicate<Product> mp){ List<Product> prods = new ArrayList<>(); for (Product prod : list){ if (mp.test(prod)){ prods.add(prod); } } return prods; }
使用lambda運算式進行過濾
@Test public void test4(){ List<Product> products = filterProductByPredicate(proList, (p) -> p.getPrice() < 8000); for (Product pro : products){ System.out.println(pro); } }
使用Stream API
// 使用jdk1.8中的Stream API進行集合的操作 @Test public void test(){ // 根據價格過濾 proList.stream() .fliter((p) -> p.getPrice() <8000) .limit(2) .forEach(System.out::println); // 根據顏色過濾 proList.stream() .fliter((p) -> "紅色".equals(p.getColor())) .forEach(System.out::println); // 遍歷輸出商品名稱 proList.stream() .map(Product::getName) .forEach(System.out::println); }
Lmabda運算式的語法總結: () -> ();
| 前置 | 語法 |
| 無引數無回傳值 | () -> System.out.println(“Hello WOrld”) |
| 有一個引數無回傳值 | (x) -> System.out.println(x) |
| 有且只有一個引數無回傳值 | x -> System.out.println(x) |
| 有多個引數,有回傳值,有多條lambda體陳述句 | (x,y) -> {System.out.println(“xxx”);return xxxx;}; |
| 有多個引數,有回傳值,只有一條lambda體陳述句 | (x,y) -> xxxx |
Stream API
stream的創建
// 1,校驗通過Collection 系列集合提供的stream()或者paralleStream() List<String> list = new ArrayList<>(); Strean<String> stream1 = list.stream(); // 2.通過Arrays的靜態方法stream()獲取陣列流 String[] str = new String[10]; Stream<String> stream2 = Arrays.stream(str); // 3.通過Stream類中的靜態方法of Stream<String> stream3 = Stream.of("aa","bb","cc"); // 4.創建無限流 // 迭代 Stream<Integer> stream4 = Stream.iterate(0,(x) -> x+2); //生成 Stream.generate(() ->Math.random());
Stream的中間操作:
/** * 篩選 過濾 去重 */ emps.stream() .filter(e -> e.getAge() > 10) .limit(4) .skip(4) // 需要流中的元素重寫hashCode和equals方法 .distinct() .forEach(System.out::println); /** * 生成新的流 通過map映射 */ emps.stream() .map((e) -> e.getAge()) .forEach(System.out::println); /** * 自然排序 定制排序 */ emps.stream() .sorted((e1 ,e2) -> { if (e1.getAge().equals(e2.getAge())){ return e1.getName().compareTo(e2.getName()); } else{ return e1.getAge().compareTo(e2.getAge()); } }) .forEach(System.out::println);
Stream的終止操作:
/** * 查找和匹配 * allMatch-檢查是否匹配所有元素 * anyMatch-檢查是否至少匹配一個元素 * noneMatch-檢查是否沒有匹配所有元素 * findFirst-回傳第一個元素 * findAny-回傳當前流中的任意元素 * count-回傳流中元素的總個數 * max-回傳流中最大值 * min-回傳流中最小值 */ /** * 檢查是否匹配元素 */ boolean b1 = emps.stream() .allMatch((e) -> e.getStatus().equals(Employee.Status.BUSY)); System.out.println(b1); boolean b2 = emps.stream() .anyMatch((e) -> e.getStatus().equals(Employee.Status.BUSY)); System.out.println(b2); boolean b3 = emps.stream() .noneMatch((e) -> e.getStatus().equals(Employee.Status.BUSY)); System.out.println(b3); Optional<Employee> opt = emps.stream() .findFirst(); System.out.println(opt.get()); // 并行流 Optional<Employee> opt2 = emps.parallelStream() .findAny(); System.out.println(opt2.get()); long count = emps.stream() .count(); System.out.println(count); Optional<Employee> max = emps.stream() .max((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary())); System.out.println(max.get()); Optional<Employee> min = emps.stream() .min((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary())); System.out.println(min.get());
還有功能比較強大的兩個終止操作 reduce和collect
reduce操作: reduce:(T identity,BinaryOperator)/reduce(BinaryOperator)-可以將流中元素反復結合起來,得到一個值
/** * reduce :規約操作 */ List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10); Integer count2 = list.stream() .reduce(0, (x, y) -> x + y); System.out.println(count2); Optional<Double> sum = emps.stream() .map(Employee::getSalary) .reduce(Double::sum); System.out.println(sum);
collect操作:Collect-將流轉換為其他形式,接收一個Collection介面的實作,用于給Stream中元素做匯總的方法
/** * collect:收集操作 */ List<Integer> ageList = emps.stream() .map(Employee::getAge) .collect(Collectors.toList()); ageList.stream().forEach(System.out::println);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/79266.html
標籤:Java
上一篇:學習一下 JVM (一) -- 了解一下 JVM 基本概念
下一篇:資料庫遷移神器——Flyway
