Lightweight-Stream-API, 对于 Java 7和下面的迭代器,重写Java 8中的流 API

分享于 

7分钟阅读

GitHub

  繁體 雙語
Stream API from Java 8 rewrited on iterators for Java 7 and below
  • 源代码名称:Lightweight-Stream-API
  • 源代码网址:http://www.github.com/aNNiMON/Lightweight-Stream-API
  • Lightweight-Stream-API源代码文档
  • Lightweight-Stream-API源代码下载
  • Git URL:
    git://www.github.com/aNNiMON/Lightweight-Stream-API.git
    Git Clone代码到本地:
    git clone http://www.github.com/aNNiMON/Lightweight-Stream-API
    Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/aNNiMON/Lightweight-Stream-API
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
    
    Lightweight-Stream-API

    Join the chat at https://gitter.im/aNNiMON/Lightweight-Stream-APIMaven CentralBuild StatusCoverage Status

    Java 8中的流API重写了 Java 7和下面的迭代器。

    包括

    • 功能接口(。SupplierFunctionConsumer 等) ;
    • Stream/IntStream/LongStream/DoubleStream (。无需并行处理,但具有多种附加方法和自定义运算符) ;
    • Optional/OptionalBoolean/OptionalInt/OptionalLong/OptionalDouble 类;
    • Exceptional 类- 处理异常的功能方法;
    • 来自 Java 7的Objects

    用法

    Stream.of(/* array | list | set | map | anything based on Iterator/Iterable interface */)
    . filter(..)
    . map(..)
     .... sorted()
    . forEach(..);Stream.of(value1, value2, value3)...IntStream.range(0, 10)...

    示例项目:https://github.com/aNNiMON/Android-Java-8-Stream-Example

    关键特性

    自定义运算符

    Java 8流不同,Lightweight-Stream-API提供了应用自定义操作符的能力。

    Stream.of(...)
    . custom(newReverse<>())
    . forEach(...);publicfinalclassReverse<T> implementsUnaryOperator<Stream<T>> {
     @OverridepublicStream<T>apply(Stream<T>stream) {
     finalIterator<? extends T> iterator = stream.getIterator();
     finalArrayDeque<T> deque =newArrayDeque<T>();
     while (iterator.hasNext()) {
     deque.addFirst(iterator.next());
     }
     returnStream.of(deque.iterator());
     }
    }

    你可以在这里找到更多的示例

    附加运算符

    除了 backported Java 8流操作符之外,库还提供:

    • filterNot - 求反 filter 运算符

      // Java 8stream.filter(((Predicate<String>) String::isEmpty).negate())// LSAstream.filterNot(String::isEmpty)
    • select - 过滤给定类的实例

      // Java 8stream.filter(Integer.class::isInstance)// LSAstream.select(Integer.class)
    • withoutNulls - 只筛选非空元素

      Stream.of("a", null, "c", "d", null)
      . withoutNulls() // [a, c, d]
    • sortBy - 通过提取函数排序

      // Java 8stream.sorted(Comparator.comparing(Person::getName))// LSAstream.sortBy(Person::getName)
    • groupBy - 通过提取函数分组

      // Java 8stream.collect(Collectors.groupingBy(Person::getName)).entrySet().stream()// LSAstream.groupBy(Person::getName)
    • chunkBy - 通过分类器函数划分排序流

      Stream.of("a", "b", "cd", "ef", "gh", "ij", "klmnn")
      . chunkBy(String::length) // [[a, b], [cd, ef, gh, ij], [klmnn]]
    • sample - 发射每个元素

      Stream.rangeClosed(0, 10)
      . sample(2) // [0, 2, 4, 6, 8, 10]
    • slidingWindow - 将流分割成固定大小的列表并在元素上滑动

      Stream.rangeClosed(0, 10)
      . slidingWindow(4, 6) // [[0, 1, 2, 3], [6, 7, 8, 9]]
    • takeWhile/dropWhile - 在 Java 9中引入,限制/跳过谓词函数

      Stream.of("a", "b", "cd", "ef", "g")
      . takeWhile(s -> s.length() ==1) // [a, b]Stream.of("a", "b", "cd", "ef", "g")
      . dropWhile(s -> s.length() ==1) // [cd, ef, g]
    • scan - 迭代应用累积函数并返回流

      IntStream.range(1, 6)
      . scan((a, b) -> a + b) // [1, 3, 6, 10, 15]
    • indexed - 向每个元素添加索引,结果是 IntPair

      Stream.of("a", "b", "c")
      . indexed() // [(0 :"a"), (1 :"b"), (2 :"c")]
    • filterIndexed/mapIndexed/takeWhileIndexed/takeUntilIndexed/dropWhileIndexed/reduceIndexed/forEachIndexed - 运算符的索引化专用化

      Stream.of("a", "b", "c")
      . mapIndexed((i, s) -> s +Integer.toString(i)) // [a0, b1, c2]

    函数

    lambda表达式中没有更丑陋的try/catch。

    // Java 8stream.map(file -> {
     try {
     returnnewFileInputStream(file);
     } catch (IOException ioe) {
     returnnull;
     }
    })// LSAstream.map(Function.Util.safe(FileInputStream::new))

    下载

    下载最新版本或者通过 Maven 获取:

    <dependency>
     <groupId>com.annimon</groupId>
     <artifactId>stream</artifactId>
     <version>1.1.9</version>
    </dependency>

    或者 Gradle:

    dependencies {
     ...
     compile 'com.annimon:stream:1.1.9'...
    }

    还包括 Java的版本。 签出 javame分支

    要在 java5.java5或者Android中使用lambda表达式,请看一下的Retrolambda存储库。


    JAVA  API  str  Java 8  迭代器  Java 7  
    相关文章