Java 8 Streams filter
在這篇教程中,我們將用少量的例子向你展示java8中的Streams filter(), collect(), findAny() and orElse().
Streams filter() 和 collect()
Java 8之前, 過濾一個集合是這樣的 :
BeforeJava8.java package com.ricky.java8; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class BeforeJava8 { public static void main(String[] args) { List<String> lines = Arrays.asList("spring", "node", "ricky"); List<String> result = getFilterOutput(lines, "ricky"); for (String temp : result) { System.out.println(temp); //output : spring, node } } private static List<String> getFilterOutput(List<String> lines, String filter) { List<String> result = new ArrayList<>(); for (String line : lines) { if (!"ricky".equals(line)) { // we dont like ricky result.add(line); } } return result; } }
輸出
spring node
1.2 java8中等價的例子,使用stream.filter()去過濾集合,使用collect()將stream轉化為一個集合。
NowJava8.java package com.ricky.java8; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class NowJava8 { public static void main(String[] args) { List<String> lines = Arrays.asList("spring", "node", "ricky"); List<String> result = lines.stream() // convert list to stream .filter(line -> !"ricky".equals(line)) // we dont like ricky .collect(Collectors.toList()); // collect the output and convert streams to a List result.forEach(System.out::println); //output : spring, node } }
-輸出
spring node
Streams filter(), findAny() and orElse()
Person.java
package com.ricky.java8;
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
//gettersm setters, toString
}
- Java 8之前, 你獲取Person的name是這樣的 :
BeforeJava8.java
package com.ricky.java8;
import java.util.Arrays;
import java.util.List;
public class BeforeJava8 {
public static void main(String[] args) {
List<Person> persons = Arrays.asList(
new Person("ricky", 30),
new Person("jack", 20),
new Person("lawrence", 40)
);
Person result = getStudentByName(persons, "jack");
System.out.println(result);
}
private static Person getStudentByName(List<Person> persons, String name) {
Person result = null;
for (Person temp : persons) {
if (name.equals(temp.getName())) {
result = temp;
}
}
return result;
}
}
輸出
```java
Person{name='jack', age=20}
```
等價的java8列子,使用stream.filter()過濾集合, .findAny().orElse (null)返回一個符合條件的物件。
NowJava8.java package com.ricky.java8; import java.util.Arrays; import java.util.List; public class NowJava8 { public static void main(String[] args) { List<Person> persons = Arrays.asList( new Person("ricky", 30), new Person("jack", 20), new Person("lawrence", 40) ); Person result1 = persons.stream() // Convert to steam .filter(x -> "jack".equals(x.getName())) // we want "jack" only .findAny() // If 'findAny' then return found .orElse(null); // If not found, return null System.out.println(result1); Person result2 = persons.stream() .filter(x -> "ahmook".equals(x.getName())) .findAny() .orElse(null); System.out.println(result2); } }
輸出
Person{name='jack', age=20} null
- 多種條件
NowJava8.java package com.ricky.java8; import java.util.Arrays; import java.util.List; public class NowJava8 { public static void main(String[] args) { List<Person> persons = Arrays.asList( new Person("ricky", 30), new Person("jack", 20), new Person("lawrence", 40) ); Person result1 = persons.stream() .filter((p) -> "jack".equals(p.getName()) && 20 == p.getAge()) .findAny() .orElse(null); System.out.println("result 1 :" + result1); //or like this Person result2 = persons.stream() .filter(p -> { if ("jack".equals(p.getName()) && 20 == p.getAge()) { return true; } return false; }).findAny() .orElse(null); System.out.println("result 2 :" + result2); } }
- 多種條件
輸出
result 1 :Person{name='jack', age=20} result 2 :Person{name='jack', age=20}
- Streams filter() and map()
NowJava8.java
package com.ricky.java8;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class NowJava8 {
public static void main(String[] args) {
List<Person> persons = Arrays.asList(
new Person("ricky", 30),
new Person("jack", 20),
new Person("lawrence", 40)
);
String name = persons.stream()
.filter(x -> "jack".equals(x.getName()))
.map(Person::getName) //convert stream to String
.findAny()
.orElse("");
System.out.println("name : " + name);
List<String> collect = persons.stream()
.map(Person::getName)
.collect(Collectors.toList());
collect.forEach(System.out::println);
}
}
輸出
name : jack ricky jack lawrence
翻譯
ricky 20170728
交流群:244930845
相關文章
- Java 8 Streams map()Java
- 8個實用的Java Streams APIJavaAPI
- Java 8 Streams 中的資料庫 CRUD 操作Java資料庫
- Java 8 Streams API:對Stream分組和分割槽JavaAPI
- java8 多條件的filter過濾JavaFilter
- STREAMS筆記(8) rule - 自定義筆記
- Java Streams 的潛力Java
- java .stream(). 使用介紹 Streams APIJavaAPI
- Java Filter過濾器JavaFilter過濾器
- CORS filter for Java applicationsCORSFilterJavaAPP
- bloom filter 的Java 版OOMFilterJava
- java中filter的用法JavaFilter
- 使用Java和Reactive Streams構建流式應用JavaReact
- Oracle StreamsOracle
- STREAMS筆記(2) 其他建立Streams的方式筆記
- 如何使用Java Streams進行資料庫查詢?Java資料庫
- Java8集合filter過濾Integer數值為空問題解決方案JavaFilter
- java.lang.ClassNotFoundException:javax.servlet.FilterJavaExceptionServletFilter
- STREAMS MONITORING
- Kafka Streams 剖析Kafka
- streams 總結
- Java安全之Filter許可權繞過JavaFilter
- Java 中的 Filter 過濾器詳解JavaFilter過濾器
- Java中stream流的filter機制理解JavaFilter
- 精讀《web streams》Web
- Streams 流處理
- Azkarra Streams簡介:Apache Kafka Streams的第一個微框架ApacheKafka框架
- 在Oracle11g Streams測試Streams資料傳輸Oracle
- 【Java基礎】--filter過濾器原理解析JavaFilter過濾器
- STREAMS筆記(1) step by step 建立一個Streams複製環境筆記
- Node.js Streams(流)Node.js
- 理解 PHP 中的 StreamsPHP
- STREAMS筆記(6) rule筆記
- Oracle Stream(2)--Streams功能Oracle
- Oracle Stream(1)--Streams概述Oracle
- 流和向量(Streams and Vectors)
- Java 8 + 10 = Java 18Java
- Java Servlet (1) —— Filter過濾請求與響應JavaServletFilter