原文鏈接http://zhhll.icu/2020/12/02/java%E5%9F%BA%E7%A1%80/java8/java%20Stream%E6%93%8D%E4%BD%9C/
java stream操作
Stream是什么
Stream又稱為流,可以將集合轉換為一種流,對集合中的每個元素進行一系列的流式操作,
資料源 ------轉換為--》流----》進行中間操作----》終止操作
多個中間操作可以連接起來形成一個流水線,除非流水線觸發終止操作,否則中間操作不會執行任何處理,在終止操作時一次性全部處理
轉化為流
使用stream()或者parallelStream()方法將集合轉為流
中間操作
篩選
filter
過濾操作,只回傳為true的資料
/**
* filter 接收lambda,從流中排除某些元素
*/
public static void testFilter(){
// 中間操作
// 使用的Predicate boolean test(T t);
Stream<String> stream = list.stream().filter(e ->
{
System.out.println("filter中間操作");
return e.equals("張三");
}
);
// 這時中間操作還沒有執行執行
System.out.println("----中間操作結束----");
//終止操作:一次執行全部操作
stream.forEach(
System.out::println
);
}
distinct
去重
/**
* distinct 篩選,通過流所生成元素的hashCode()和equals()方法去重
*/
public static void testDistinct(){
list.stream().distinct().forEach(System.out::println);
}
切片
limit
回傳前n個元素
/**
* limit 截斷流,使其元素不超過給定數量
*/
public static void testLimit(){
list.stream().limit(2).forEach(System.out::println);
}
skip
去除(跳過)前n個元素
/**
* skip 跳過元素,回傳一個扔掉了前n個元素的流,若流中元素不足n個,則回傳空流
*/
public static void testSkip(){
list.stream().skip(2).forEach(System.out::println);
}
public static void testLimitAndSkip(){
System.out.println("--------先limit再skip---------");
list.stream().limit(2).skip(1).forEach(System.out::println);
System.out.println("--------先skip再limit---------");
list.stream().skip(1).limit(2).forEach(System.out::println);
}
注意:在limit和skip搭配使用的時候,兩個的順序不同會導致結果不同
- 先進行limit,再進行skip時,會選擇前兩個資料,然后在跳過第一個資料,只會篩選出一條資料
- 先進行skip,再進行limit時,會先跳過一條資料,在選擇剩下資料的前兩條,最侄訓篩選出兩條資料
排序
sorted
排序可以有兩種排序方式,第一種是進行排序的類要實作Comparable介面,第二種是在自己實作一個Comparator介面
/**
* sorted()自然排序 Comparable 所要排序的類必須實作Comparable介面
*/
public static void test(){
list.stream().map(User::getAge).sorted().forEach(System.out::println);
}
/**
* sorted(Comparator com) 定制排序(Comparator)
*/
public static void test1(){
list.stream().sorted(
(o1, o2) -> {
if(o1.getAge() > o2.getAge()){
return -1;
}
return 0;
}
).forEach(System.out::println);
}
映射
map
/**
* map 接收lambda,將元素轉換為其他形式或提取資訊,接收一個函式作為函式,
* 該函式會被應用到每個元素上,并將其映射成一個新的元素
*
* 如果函式回傳的是一個流的話,使用map會使得流里存盤著多個流
*/
public static void testMap(){
// 使用Function R apply(T t);
list.stream().map(User::getAge).forEach(System.out::println);
}
flatmap
/**
* flatMap 接收一個函式作為引數,將流中的每個值都換成另一個流,然后把所有流連接成一個流
*
* 如果函式回傳的是一個流,使用flatMap會使得函式回傳的流中的元素放到一個流中
*/
public static void testFlatMap(){
// 要求Function R apply(T t);中回傳值是一個Stream流
List<String> add = new ArrayList<>();
add.add("添加元素");
List<String> strings = list.stream().map(User::getName).collect(Collectors.toList());
strings.stream().flatMap(
TestStreamApi1::joinStream
).forEach(System.out::println);
}
終止操作
allMatch
流中所有元素都要匹配給定的條件為true,否則為false 相當于且
/**
* allMatch測驗
*/
public static void testAllMatch(){
boolean isSex = list.stream().allMatch(
l -> l.getSex() == 0
);
System.out.println(isSex);
}
anyMatch
流中有任意一條資料匹配給定的條件為true,否則為false 相當于并
/**
* anyMatch測驗
*/
public static void testAnyMatch(){
boolean isSex = list.stream().anyMatch(
l -> l.getSex() == 0
);
System.out.println(isSex);
}
noneMatch
流中所有的資料都不匹配給定條件時為true,否則為false 相當于非
/**
* noneMatch測驗
*/
public static void testNoneMatch(){
boolean isSex = list.stream().noneMatch(
l -> l.getSex() == 0
);
System.out.println(isSex);
}
findFirst
找到第一個元素
/**
* findFirst測驗
*/
public static void testFindFirst(){
User user = list.stream().sorted(
((o1, o2) -> {
if(o1.getAge() > o2.getAge()){
return -1;
} else if(o1.getAge() < o2.getAge()){
return 1;
}
return 0;
})
).findFirst().get();
System.out.println(user);
}
findAny
找到其中任意一個元素
/**
* findAny測驗
*/
public static void testFindAny(){
User user = list.stream().filter(
l -> l.getSex() == 0
).findAny().get();
System.out.println(user);
}
count
回傳流中元素的數量
/**
* count測驗
*/
public static void testCount(){
long count = list.stream().count();
System.out.println(count);
}
max
回傳流中根據比較之后的最大值元素
/**
* max測驗
*/
public static void testMax(){
User user = list.stream().max(
((o1, o2) -> {
if(o1.getAge() > o2.getAge()){
return 1;
} else if(o1.getAge() < o2.getAge()){
return -1;
}
return 0;
})
).get();
System.out.println(user);
}
min
回傳流中根據比較之后的最小值元素
/**
* min測驗
*/
public static void testMin(){
User user = list.stream().min(
((o1, o2) -> {
if(o1.getAge() > o2.getAge()){
return 1;
} else if(o1.getAge() < o2.getAge()){
return -1;
}
return 0;
})
).get();
System.out.println(user);
}
歸約和收集
歸約reduce
使用reduce來進行運算
/**
* 歸約 將流中元素反復結合起來,得到一個值
* reduce(T identity, BinaryOperator<T> accumulator) /BinaryOperator<T> accumulator/U identity,
* BiFunction<U, ? super T, U> accumulator,
* BinaryOperator<U> combiner
*/
public static void testReduce(){
List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9);
// BinaryOperator 二元運算 R apply(T t, U u);
int sum = list.stream().reduce((x, y) -> x+y).get();
System.out.println(sum);
}
收集collect
根據不同的收集器collect(Collectors.toList())、collect(Collectors.toSet())來回傳不同的集合
/**
* 收集 collect -- 將流轉換為其他形式 接收一個Collector介面的實作,用于給Stream中元素做匯總的方法
*/
public static void testCollect(){
// Collector是一個介面 有一個Collectors提供了各種轉換方式
List<String> strings = list.stream().map(User::getName).collect(Collectors.toList());
System.out.println(strings);
}
由于本身的博客百度沒有收錄,博客地址http://zhhll.icu
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/249686.html
標籤:Java
上一篇:JetBrains推出新字體,專為開發人員設計,堪稱最適合程式員編程的字體
下一篇:RabbitMQ作業模式詳解
