stream拓展

简介: stream拓展

开成花灾的玫瑰不是灿烂,而是荒凉。——严歌苓

源码地址:https://gitee.com/VampireAchao/stream-query/blob/master/stream-core/src/main/java/io/github/vampireachao/stream/core/stream/Steam.java

测试用例:

https://gitee.com/VampireAchao/stream-query/blob/master/stream-core/src/test/java/io/github/vampireachao/stream/core/stream/SteamTest.java

package io.github.vampireachao.stream.core.stream;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.*;
import java.util.stream.Stream;
import static java.util.Collections.singletonList;
/**
 * @author VampireAchao
 * @since 2022/7/19 14:14
 */
class SteamTest {
    @Test
    void testBuilder() {
        List<Integer> list = Steam.<Integer>builder().add(1).add(2).add(3).build().toList();
        Assertions.assertEquals(Arrays.asList(1, 2, 3), list);
    }
    @Test
    void testOf() {
        Assertions.assertEquals(3, Steam.of(Arrays.asList(1, 2, 3), true).count());
        Assertions.assertEquals(3, Steam.of(1, 2, 3).count());
        Assertions.assertEquals(3, Steam.of(Stream.builder().add(1).add(2).add(3).build()).count());
    }
    @Test
    void testSplit() {
        List<Integer> list = Steam.split("1,2,3", ",").map(Integer::valueOf).toList();
        Assertions.assertEquals(Arrays.asList(1, 2, 3), list);
    }
    @Test
    void testIterator() {
        List<Integer> list = Steam.iterate(0, i -> i < 3, i -> ++i).toList();
        Assertions.assertEquals(Arrays.asList(0, 1, 2), list);
    }
    @Test
    void testToCollection() {
        List<Integer> list = Arrays.asList(1, 2, 3);
        List<String> toCollection = Steam.of(list).map(String::valueOf).toColl(LinkedList::new);
        Assertions.assertEquals(Arrays.asList("1", "2", "3"), toCollection);
    }
    @Test
    void testToList() {
        List<Integer> list = Arrays.asList(1, 2, 3);
        List<String> toList = Steam.of(list).map(String::valueOf).toList();
        Assertions.assertEquals(Arrays.asList("1", "2", "3"), toList);
    }
    @Test
    void testToSet() {
        List<Integer> list = Arrays.asList(1, 2, 3);
        Set<String> toSet = Steam.of(list).map(String::valueOf).toSet();
        Assertions.assertEquals(new HashSet<>(Arrays.asList("1", "2", "3")), toSet);
    }
    @Test
    void testToZip() {
        List<Integer> orders = Arrays.asList(1, 2, 3);
        List<String> list = Arrays.asList("dromara", "hutool", "sweet");
        Map<Integer, String> toZip = Steam.of(orders).toZip(list);
        Assertions.assertEquals(new HashMap<Integer, String>() {{
            put(1, "dromara");
            put(2, "hutool");
            put(3, "sweet");
        }}, toZip);
    }
    @Test
    void testJoin() {
        List<Integer> list = Arrays.asList(1, 2, 3);
        String joining = Steam.of(list).join();
        Assertions.assertEquals("123", joining);
        Assertions.assertEquals("1,2,3", Steam.of(list).join(","));
        Assertions.assertEquals("(1,2,3)", Steam.of(list).join(",", "(", ")"));
    }
    @Test
    void testToMap() {
        List<Integer> list = Arrays.asList(1, 2, 3);
        Map<String, Integer> identityMap = Steam.of(list).toMap(String::valueOf);
        Assertions.assertEquals(new HashMap<String, Integer>() {{
            put("1", 1);
            put("2", 2);
            put("3", 3);
        }}, identityMap);
    }
    @Test
    void testGroup() {
        List<Integer> list = Arrays.asList(1, 2, 3);
        Map<String, List<Integer>> group = Steam.of(list).group(String::valueOf);
        Assertions.assertEquals(
                new HashMap<String, List<Integer>>() {{
                    put("1", singletonList(1));
                    put("2", singletonList(2));
                    put("3", singletonList(3));
                }}, group);
    }
    @Test
    void testMapIdx() {
        List<String> list = Arrays.asList("dromara", "hutool", "sweet");
        List<String> mapIndex = Steam.of(list).mapIdx((e, i) -> i + 1 + "." + e).toList();
        Assertions.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), mapIndex);
        // 并行流时为-1
        Assertions.assertEquals(Arrays.asList(-1, -1, -1), Steam.of(1, 2, 3).parallel().mapIdx((e, i) -> i).toList());
    }
    @Test
    void testMapMulti() {
        List<Integer> list = Arrays.asList(1, 2, 3);
        List<Integer> mapMulti = Steam.of(list).<Integer>mapMulti((e, buffer) -> {
            if (e % 2 == 0) {
                buffer.accept(e);
            }
            buffer.accept(e);
        }).toList();
        Assertions.assertEquals(Arrays.asList(1, 2, 2, 3), mapMulti);
    }
    @Test
    void testDistinct() {
        List<Integer> list = Arrays.asList(1, 2, 2, 3);
        List<Integer> distinctBy = Steam.of(list).distinct(String::valueOf).toList();
        Assertions.assertEquals(Arrays.asList(1, 2, 3), distinctBy);
    }
    @Test
    void testForeachIdx() {
        List<String> list = Arrays.asList("dromara", "hutool", "sweet");
        Steam.Builder<String> builder = Steam.builder();
        Steam.of(list).forEachIdx((e, i) -> builder.accept(i + 1 + "." + e));
        Assertions.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), builder.build().toList());
        // 并行流时为-1
        Steam.of(1, 2, 3).parallel().forEachIdx((e, i) -> Assertions.assertEquals(-1, i));
    }
    @Test
    void testForEachOrderedIdx() {
        List<String> list = Arrays.asList("dromara", "hutool", "sweet");
        Steam.Builder<String> builder = Steam.builder();
        Steam.of(list).forEachOrderedIdx((e, i) -> builder.accept(i + 1 + "." + e));
        Assertions.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), builder.build().toList());
    }
    @Test
    void testFlatMapIdx() {
        List<String> list = Arrays.asList("dromara", "hutool", "sweet");
        List<String> mapIndex = Steam.of(list).flatMapIdx((e, i) -> Steam.of(i + 1 + "." + e)).toList();
        Assertions.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), mapIndex);
        // 并行流时为-1
        Assertions.assertEquals(Arrays.asList(-1, -1, -1), Steam.of(1, 2, 3).parallel().mapIdx((e, i) -> i).toList());
    }
    @Test
    void testFlatMapIter() {
        List<Integer> list = Arrays.asList(1, 2, 3);
        List<Integer> flatMapIter = Steam.of(list).<Integer>flatMapIter(e -> null).toList();
        Assertions.assertEquals(Collections.emptyList(), flatMapIter);
    }
    @Test
    void testFilter() {
        List<Integer> list = Arrays.asList(1, 2, 3);
        List<Integer> filterIndex = Steam.of(list).filter(String::valueOf, "1").toList();
        Assertions.assertEquals(Collections.singletonList(1), filterIndex);
    }
    @Test
    void testFilterIdx() {
        List<String> list = Arrays.asList("dromara", "hutool", "sweet");
        List<String> filterIndex = Steam.of(list).filterIdx((e, i) -> i < 2).toList();
        Assertions.assertEquals(Arrays.asList("dromara", "hutool"), filterIndex);
        // 并行流时为-1
        Assertions.assertEquals(3L, Steam.of(1, 2, 3).parallel().filterIdx((e, i) -> i == -1).count());
    }
    @Test
    void testNonNull() {
        List<Integer> list = Arrays.asList(1, null, 2, 3);
        List<Integer> nonNull = Steam.of(list).nonNull().toList();
        Assertions.assertEquals(Arrays.asList(1, 2, 3), nonNull);
    }
    @Test
    void testParallel() {
        Assertions.assertTrue(Steam.of(1, 2, 3).parallel(true).isParallel());
        Assertions.assertFalse(Steam.of(1, 2, 3).parallel(false).isParallel());
    }
    @Test
    void testPush() {
        List<Integer> list = Arrays.asList(1, 2);
        List<Integer> push = Steam.of(list).push(3).toList();
        Assertions.assertEquals(Arrays.asList(1, 2, 3), push);
    }
    @Test
    void testUnshift() {
        List<Integer> list = Arrays.asList(2, 3);
        List<Integer> unshift = Steam.of(list).unshift(1).toList();
        Assertions.assertEquals(Arrays.asList(1, 2, 3), unshift);
    }
    @Test
    void testAt() {
        List<Integer> list = Arrays.asList(1, 2, 3);
        Assertions.assertEquals(1, Steam.of(list).at(0));
        Assertions.assertEquals(1, Steam.of(list).at(-3));
        Assertions.assertEquals(3, Steam.of(list).at(-1));
        Assertions.assertNull(Steam.of(list).at(-4));
    }
    @Test
    void testSplice() {
        List<Integer> list = Arrays.asList(1, 2, 3);
        Assertions.assertEquals(Arrays.asList(1, 2, 2, 3), Steam.of(list).splice(1, 0, 2).toList());
        Assertions.assertEquals(Arrays.asList(1, 2, 3, 3), Steam.of(list).splice(3, 1, 3).toList());
        Assertions.assertEquals(Arrays.asList(1, 2, 4), Steam.of(list).splice(2, 1, 4).toList());
        Assertions.assertEquals(Arrays.asList(1, 2), Steam.of(list).splice(2, 1).toList());
        Assertions.assertEquals(Arrays.asList(1, 2, 3), Steam.of(list).splice(2, 0).toList());
        Assertions.assertEquals(Arrays.asList(1, 2), Steam.of(list).splice(-1, 1).toList());
        Assertions.assertEquals(Arrays.asList(1, 2, 3), Steam.of(list).splice(-2, 2, 2, 3).toList());
    }
    @Test
    void testFindFirst() {
        List<Integer> list = Arrays.asList(1, 2, 3);
        Integer find = Steam.of(list).findFirst(Objects::nonNull);
        Assertions.assertEquals(1, find);
    }
    @Test
    void testFindFirstIdx() {
        List<Integer> list = Arrays.asList(null, 2, 3);
        Integer idx = Steam.of(list).findFirstIdx(Objects::nonNull);
        Assertions.assertEquals(1, idx);
        Assertions.assertEquals(-1, Steam.of(list).parallel().findFirstIdx(Objects::nonNull));
    }
    @Test
    void testFindLast() {
        List<Integer> list = Arrays.asList(1, null, 3);
        Integer find = Steam.of(list).findLast(Objects::nonNull);
        Assertions.assertEquals(3, find);
        Assertions.assertEquals(3, Steam.of(list).findLast().orElse(null));
    }
    @Test
    void testFindLastIdx() {
        List<Integer> list = Arrays.asList(1, null, 3);
        Integer idx = Steam.of(list).findLastIdx(Objects::nonNull);
        Assertions.assertEquals(2, idx);
        Assertions.assertEquals(-1, Steam.of(list).parallel().findLastIdx(Objects::nonNull));
    }
    @Test
    void testZip() {
        List<Integer> orders = Arrays.asList(1, 2, 3);
        List<String> list = Arrays.asList("dromara", "hutool", "sweet");
        List<String> zip = Steam.of(orders).zip(list, (e1, e2) -> e1 + "." + e2).toList();
        Assertions.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), zip);
    }
}
相关文章
|
6月前
|
Java API 数据处理
探索 Java 8 中的 Stream 流:构建流的多种方式
探索 Java 8 中的 Stream 流:构建流的多种方式
|
2月前
|
安全 Java 测试技术
总结|Stream流技术在真实案例中的应用
本文不仅提供了理论上的讲解,还通过实际代码示例展示了如何应用Stream API来解决常见的编程问题。
|
5月前
|
SQL 前端开发 算法
JavaEE企业级开发中常用的Stream流
JavaEE企业级开发中常用的Stream流
37 2
|
5月前
|
C++
C++对C的改进和拓展:I/O 流
C++对C的改进和拓展:I/O 流
23 1
|
4月前
|
存储 Java BI
Java基础之stream流最新版,stream流的基本操作
Java基础之stream流最新版,stream流的基本操作
50 0
|
5月前
|
存储 Java API
Java——Stream流(1/2):Stream流入门、Stream流的创建(认识Stream、体验Stream流、Stream流的使用步骤、获取Stream流的方法)
Java——Stream流(1/2):Stream流入门、Stream流的创建(认识Stream、体验Stream流、Stream流的使用步骤、获取Stream流的方法)
79 0
|
5月前
|
Java
Java——Stream流(2/2):Stream流的中间方法、终结方法(方法、案例演示)
Java——Stream流(2/2):Stream流的中间方法、终结方法(方法、案例演示)
63 0
超强的Stream流了解一下
超强的Stream流了解一下
|
6月前
|
Java API 数据处理
JDK 8:Stream API——数据处理的新篇章
JDK 8引入了Stream API,为Java中的数据处理提供了一种全新的方式。本文将详细介绍Stream API的原理、优势以及如何在实际开发中应用这一特性。
|
11月前
|
Java 数据处理 数据库
stream-query加入dromara开源组织
stream-query加入dromara开源组织
82 1