java8 Stream 所有函數(shù)demo

所有函數(shù):

allMatch 和 anyMatch

/**
 * allMatch 和 anyMatch
 * 這個并不是過濾數(shù)據(jù)用的,而只是獲取一個判斷結(jié)果
 *
 * 注意:這個函數(shù)一個Stream只能調(diào)用一次,調(diào)用一次后,這個Stream就調(diào)用close,也就是流就會關(guān)閉了,
 * 如果還要使用,那么需要再開辟一個
 */
@Test
public void allMatchAndAnyMatchTest(){
    List<Integer> integerList = Arrays.asList(1,2,3,123,12,41);
    Stream<Integer> stream = integerList.stream();

    //true
    show(stream.allMatch(t->t>0));

    Stream<Integer> stream1 = integerList.stream();
    //false
    show(stream1.allMatch(t->t>8));

    Stream<Integer> stream3 = integerList.stream();
    //true
    show(stream3.anyMatch(t->t>8));

    Stream<Integer> stream4 = integerList.stream();
    //false
    show(stream4.anyMatch(t->t>400));
}

builder

/**
 * Stream居然還有建造者模式,數(shù)據(jù)是空的
 */
@Test
public void builderTest(){
    Stream<Object> stringStream = Stream.builder().build();
    stringStream.forEach(Out::show);
}

collect

/**
 * collect 這個算是流的終止操作符
 */
@Test
public void collectTest(){
    List<Integer> integerList = Arrays.asList(1,2,3,123,12,41);
    integerList.stream().collect(Collectors.toList()).forEach(Out::show);
}

contact

/**
 * static contact 連接兩個流的操作
 */
@Test
public void contactTest(){
    List<Integer> list = Arrays.asList(1,2,3);
    List<Integer> list2 = Arrays.asList(3,123,12,41);

    Stream.concat(list.stream(), list2.stream()).forEach(Out::show);
}

count

/**
 * count
 */
@Test
public void CountTest(){
    List<Integer> list = Arrays.asList(1,2,3);
    show(list.stream().count());
}

distinct

/**
 * distinct 數(shù)據(jù)流的唯一性
 */
@Test
public void distinctTest(){
    List<Integer> list = Arrays.asList(1,2,3);
    List<Integer> list2 = Arrays.asList(3,123,12,41);

    Stream.concat(list.stream(), list2.stream()).distinct().forEach(Out::show);
}

dropWhile

/**
 * dropWhile
 * 排序后的數(shù)據(jù)流,這個判斷僅判斷一次,一旦遇到不滿足,則后面的就不再判斷,和takeWhile相似
 */
@Test
public void dropWhileTest(){
    Stream.of(12,3,4,65,4).sorted(Comparator.reverseOrder()).dropWhile(t->t>5).forEach(Out::show);
}

skip

/**
 * skip 跳過前面多少個元素
 */
@Test
public void skipTest(){
    List<Integer> list = Arrays.asList(1,2,3,123,12,41);
    list.stream().skip(3).forEach(Out::show);
}

limit

/**
 * limit 保留流中的從前往后的多少個元素
 */
@Test
public void limitTest(){
    List<Integer> list = Arrays.asList(1,2,3,123,12,41);
    list.stream().limit(4).forEach(Out::show);
}

empty

/**
 * static empty 返回一個空的集合
 */
@Test
public void emptyTest(){
    Stream.empty().forEach(Out::show);
}

filter

/**
 * filter 過濾對應(yīng)的數(shù)據(jù)
 */
@Test
public void filterTest(){
    List<Integer> list = Arrays.asList(1,2,3,123,12,41);
    list.stream().filter(t->t>10).forEach(Out::show);
}

findAny

/**
 * findAny 主要用于過濾后查找某一個數(shù)據(jù)
 */
@Test
public void findAnyTest(){
    List<Integer> list = Arrays.asList(1,2,3,123,12,41);
    show(list.stream().filter(t->t==14).findAny().orElse(0));
}

findFirst

/**
 * findFirst
 */
@Test
public void findFirstTest(){
    List<Integer> list = Arrays.asList(1,2,3,123,12,41);
    show(list.stream().filter(t->t>14).findFirst().orElse(0));
}

flatMap

/**
 * flatMap 主要用于將List< List< >> 這種多重嵌套轉(zhuǎn)換為一層,跟CompletableFuture.thenCompose比較像
 */
@Test
public void flatMapTest(){
    List<Integer> list1 = Arrays.asList(1,2);
    List<Integer> list2 = Arrays.asList(1,2);
    List<Integer> list3 = Arrays.asList(1,2);

    List<List<Integer>> lists = List.of(list1, list2, list3);
    List<Integer> result = lists.stream().flatMap(Collection::stream).collect(Collectors.toList());
}

forEachOrdered

/**
 * forEachOrdered 查詢數(shù)據(jù)
 * 與forEach 的區(qū)別在于在并行運(yùn)行的時候,forEach是并行亂序的,而forEachOrdered還是有順序的
 */
@Test
public void forEachOrderedTest(){
    List<Integer> list = Arrays.asList(1,2,3,123,12,41);

    list.stream().parallel().forEach(Out::show);
    tab();
    list.stream().parallel().forEachOrdered(Out::show);
}

generate

/**
 * generate 用于生成流數(shù)據(jù)
 * 但是這個函數(shù)生成的數(shù)據(jù)是無限的,因此必須用limit進(jìn)行限制,該函數(shù)主要用于生成自己的流
 */
@Test
public void generateTest(){
    Random seed = new Random();
    Supplier<Integer> random = seed::nextInt;
    Stream.generate(random).limit(12).forEach(Out::show);

    //函數(shù)會一直執(zhí)行,直到limit限制,否則會一直阻塞
    Stream.generate(()->23).limit(12).forEach(Out::show);
}

iterate

/**
 * iterate 用于生成流數(shù)據(jù)
 */
@Test
public void iterateTest(){
    //方法1
    Stream.iterate(0, a->a+2).limit(3).forEach(Out::show);

    //方法2
    Stream.iterate(0, r->r<8, a->a+2).limit(7).forEach(Out::show);
}

max

/**
 * max
 */
@Test
public void maxTest(){
    show(Stream.iterate(0, a->a+1).limit(12).max(Comparator.comparingInt(a -> a)).get());
}

noneMatch

/**
 * noneMatch 測試
 */
@Test
public void noneMatchTest(){
    show(Stream.iterate(0, a->a+1).limit(12).noneMatch(a->a>3));
    show(Stream.iterate(0, a->a+1).limit(12).noneMatch(a->a>30));
}

of

/**
 * of 測試
 */
@Test
public void ofTest(){
    Stream.of(12).forEach(Out::show);
    tab();
    Stream.of(12,3,4,65,4).forEach(Out::show);
}

ofNullable

/**
 * ofNullable
 * 與of 區(qū)別在于ofNullable是能夠存放null
 */
@Test
public void ofNullableTest(){
    Stream.ofNullable(12).forEach(Out::show);
    Stream.ofNullable(null).forEach(Out::show);
}

peek

/**
 * peek
 * 這個是返回一個新的流,但是并沒有對流中的數(shù)據(jù)進(jìn)行操作,和map的區(qū)別在于map會生成一個新的數(shù)據(jù)流
 * peek和foreach的區(qū)別在于peek是中間數(shù)據(jù),而forEach是Stream終結(jié)流函數(shù)
 */
@Test
public void peekTest(){
    Stream.of(12,3,4,65,4)
        .peek(t->{
            show(" - "+t+" - ");
        })
        .filter(t->t>6)
        .peek(t->{
            show(" * "+t+" * ");
        })
        .forEach(Out::show);
}

reduce

/**
 * reduce 用法
 * reduce 這個其實(shí)是相當(dāng)于將那些流全部進(jìn)行匯總之后進(jìn)行操作
 */
@Test
public void reduceTest(){
    show(Stream.of(12,3,4,65,4).reduce(1, (a, b)->a+b));
    show(Stream.of(12,3,4,65,4).reduce((a, b)->a+b));
}

sorted

/**
 * sorted
 * 默認(rèn)升序
 */
@Test
public void sortedTest(){
    Stream.of(12,3,4,65,4).sorted().forEach(Out::show);
    Stream.of(12,3,4,65,4).sorted(Comparator.reverseOrder()).forEach(Out::show);
}

takeWhile

/**
 * takeWhile java9
 * 該函數(shù)用于對排序后的數(shù)據(jù)進(jìn)行過濾,從頭排序,直到不滿足條件,這個并不是過濾所有的,跟函數(shù)dropWhile
 */
@Test
public void takeWhileTest(){
    Stream.of(12,3,4,65,4).sorted(Comparator.reverseOrder()).takeWhile(t->t>5).forEach(Out::show);
}

toArray

/**
 * toArray
 * 將數(shù)據(jù)流轉(zhuǎn)換為數(shù)組
 */
@Test
public void toArrayTest(){
    //方法1
    Object[] objects = Stream.of(12,3,4,65,4).toArray();
    Arrays.stream(objects).forEach(Out::show);

    //方法2
    Integer[] integers = Stream.of(12,3,4,65,4).toArray(Integer[]::new);
    Arrays.stream(integers).forEach(Out::show);
}

<br />

注意:

1.java9的這個dropWhite函數(shù)和filter函數(shù)有什么區(qū)別

這個函數(shù)需要排序,排序后,這兩個函數(shù)都是遇到一次不滿足的,則不再向后過濾,所以性能稍微會高一點(diǎn)<br />

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容