package basic;
import org.junit.Before;
import org.junit.Test;
import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;
/**
* @author 海棠無香
* @since 2022/10/30 7:01 下午
*/
public class StreamTest {
public final static String MIDDLE_OPERATE = "中間操作";
public final static String END_OPERATE = "終值操作";
public static class CustomStream {
public String operateName;
public Integer id;
public String describe;
public String type;
public CustomStream(String operateName, Integer id, String describe, String type) {
this.operateName = operateName;
this.id = id;
this.describe = describe;
this.type = type;
}
@Override
public String toString() {
return "Stream{" +
"operateName='" + operateName + '\'' +
", id=" + id +
", describe='" + describe + '\'' +
", type='" + type + '\'' +
'}' + "\n";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CustomStream stream = (CustomStream) o;
return Objects.equals(operateName, stream.operateName) && Objects.equals(id, stream.id) && Objects.equals(describe, stream.describe) && Objects.equals(type, stream.type);
}
@Override
public int hashCode() {
return Objects.hash(operateName, id, describe, type);
}
}
private List<CustomStream> streams;
public static void main(String[] args) {
System.out.println("Stream Test");
}
@Before
public void beforeTest() {
this.streams = new ArrayList<>();
this.streams.add(new CustomStream("filter", 1, "過濾", StreamTest.MIDDLE_OPERATE));
this.streams.add(new CustomStream("distinct", 2, "去重-根據(jù)hashCode和equals方法對比", StreamTest.MIDDLE_OPERATE));
this.streams.add(new CustomStream("limit", 3, "數(shù)據(jù)量", StreamTest.MIDDLE_OPERATE));
this.streams.add(new CustomStream("skip", 4, "去掉前幾個", StreamTest.MIDDLE_OPERATE));
this.streams.add(new CustomStream("map", 5, "類型修改", StreamTest.MIDDLE_OPERATE));
this.streams.add(new CustomStream("peak", 6, "遍歷數(shù)據(jù)悼嫉,修改數(shù)據(jù)等", StreamTest.MIDDLE_OPERATE));
this.streams.add(new CustomStream("flatMap", 7, "將每個流的內(nèi)容拼接起來,進(jìn)行扁平化操作", StreamTest.MIDDLE_OPERATE));
this.streams.add(new CustomStream("collect", 11, "收集:轉(zhuǎn)為其他格式", StreamTest.END_OPERATE));
this.streams.add(new CustomStream("anyMatch", 12, "任意一個元素滿足條件", StreamTest.END_OPERATE));
this.streams.add(new CustomStream("allMatch", 13, "任意全部元素都滿足條件", StreamTest.END_OPERATE));
this.streams.add(new CustomStream("noneMatch", 14, "任意一個元素都不滿足條件", StreamTest.END_OPERATE));
this.streams.add(new CustomStream("findAny", 15, "返回流中任意一個元素:Optional對象", StreamTest.END_OPERATE));
this.streams.add(new CustomStream("findFirst", 16, "返回流中第一個元素", StreamTest.END_OPERATE));
this.streams.add(new CustomStream("forEach", 17, "循環(huán)操作", StreamTest.END_OPERATE));
this.streams.add(new CustomStream("count", 18, "數(shù)量", StreamTest.END_OPERATE));
this.streams.add(new CustomStream("reduce", 19, "將結(jié)果依次整合", StreamTest.END_OPERATE));
this.streams.add(new CustomStream("min", 20, "最大", StreamTest.END_OPERATE));
this.streams.add(new CustomStream("max", 21, "最小", StreamTest.END_OPERATE));
System.out.println(Objects.equals(this.streams.get(0), this.streams.get(2)));
}
/**
* stream 是流式操作
*/
@Test
public void testStream() {
List<CustomStream> collect = this.streams.stream()
.filter(i -> i.id > 1)
.distinct()
.skip(2)
.limit(4)
.map(i -> {
System.out.println("map---");
// 類型轉(zhuǎn)換 -- 正常應(yīng)該使用 peak陋率,此處只演示用
i.id = i.id * 10;
return i;
})
.sorted((a, b) -> b.id - a.id) // 倒序
.peek(i -> System.out.println("peak--"))
.collect(Collectors.toList());
System.out.println(collect);
System.out.println("-----------------");
System.out.println(this.streams);
}
/**
* 測試 flatMap
*/
@Test
public void testFlatMap() {
List<CustomStream> s1 = new ArrayList<>();
s1.add(this.streams.get(0));
s1.add(this.streams.get(1));
s1.add(this.streams.get(2));
List<CustomStream> s2 = new ArrayList<>();
s2.add(this.streams.get(2));
s2.add(this.streams.get(3));
s2.add(this.streams.get(4));
List<List<CustomStream>> lists = new ArrayList<>();
lists.add(s1);
lists.add(s2);
List<CustomStream> collect = lists.stream().flatMap(Collection::stream).filter(i -> i.id > 2).collect(Collectors.toList());
System.out.println(collect);
}
@Test
public void testEnd() {
boolean b = this.streams.stream().peek(System.out::println).anyMatch(i -> i.id > 3);
System.out.println(b);
Map<Integer, List<CustomStream>> collect = this.streams.stream().collect(Collectors.groupingBy(i -> i.id));
System.out.println(collect);
Integer reduce = this.streams.stream().map(i -> i.id).reduce(0, Integer::sum);
System.out.println(reduce);
}
@Test
public void testGroup() {
// 分組統(tǒng)計類型數(shù)量
Map<String, Integer> collect = this.streams.stream().collect(Collectors.toMap(i -> i.type, v -> 1, (o, n) -> o + 1));
// Collectors.toMap 4個參數(shù)依次是:key田绑、value拉讯、key 重復(fù)時的value設(shè)置操作竭望、map類型
TreeMap<String, Integer> collect5 = this.streams.stream().collect(Collectors.toMap(i -> i.type, v -> 1, (o, n) -> o + 1, TreeMap::new));
// 獲取type值重復(fù)出現(xiàn)次數(shù)在1次之上的內(nèi)容
List<String> collect4 = this.streams.stream().collect(Collectors.toMap(i -> i.type, v -> 1, (o, n) -> o + 1))
.entrySet()
.stream()
.filter(i -> i.getValue() > 1)
.map(Map.Entry::getKey)
.collect(Collectors.toList());
System.out.println(collect4);
// 按類型進(jìn)行分組: 一個參數(shù):分組的key
Map<String, List<CustomStream>> collect1 = this.streams.stream().collect(Collectors.groupingBy(i -> i.type));
// 分組后底瓣,進(jìn)行統(tǒng)計:求和浅辙、平均值或者再分組等操作:兩個參數(shù):第一個參數(shù)是分組的key扭弧,第二個參數(shù)是分組后的數(shù)據(jù)收集方式
Map<String, Integer> collect2 = this.streams.stream().collect(Collectors.groupingBy(i -> i.type, Collectors.summingInt(i -> i.id)));
// 指定map類型,默認(rèn)是hashMap:三個參數(shù):第一個參數(shù)是分組的key记舆,第二個參數(shù)是map的類型鸽捻,第三個參數(shù)是分組后的數(shù)據(jù)收集方式
TreeMap<String, Integer> collect3 = this.streams.stream().collect(Collectors.groupingBy(i -> i.type, TreeMap::new, Collectors.summingInt(i -> i.id)));
System.out.println(collect3);
}
}
Stream流式操作
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來续崖,“玉大人敲街,你說我怎么就攤上這事⊙贤” “怎么了多艇?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長像吻。 經(jīng)常有香客問我峻黍,道長复隆,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任姆涩,我火速辦了婚禮挽拂,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘骨饿。我一直安慰自己亏栈,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開白布宏赘。 她就那樣靜靜地躺著绒北,像睡著了一般。 火紅的嫁衣襯著肌膚如雪察署。 梳的紋絲不亂的頭發(fā)上镇饮,一...
- 文/蒼蘭香墨 我猛地睜開眼聂喇,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了蔚携?” 一聲冷哼從身側(cè)響起希太,我...
- 正文 年R本政府宣布喷斋,位于F島的核電站唁毒,受9級特大地震影響蒜茴,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜枉证,卻給世界環(huán)境...
- 文/蒙蒙 一矮男、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧室谚,春花似錦毡鉴、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至入篮,卻和暖如春陈瘦,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背潮售。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 原文鏈接:blog.ouyangsihai.cn >> Java8 的 Stream 流式操作之王者歸來 相對于J...
- 作者:湯圓 個人博客:javalover.cc[http://www.javalover.cc] 前言 之前總是朋...
- Java8 的出現(xiàn)可以說是 Java 面向函數(shù)式編程前進(jìn)的一大步。刨除了很多冗余操作训枢,間接的解放了雙手?? Java...