添加方法到 Java 8 流
Add a Method to Java 8 Stream
在使用 Java 8 Streams, I some times find that Stream doesn't have a particular method that I desire (e.g. takeWhile(), dropWhile(), skipLast()
). How do I create my own stream class which has the additional methods with out re-writing the entire Java 8 Stream 架构时?
我知道 StreamEx library and know that it has takeWhile() and dropWhile(). As of writing this, it doesn't have skipLast()
. I have filed an issue 这个方法。
自版本 0.5.4 StreamEx library has a chain()
方法。这允许创建可以轻松插入的辅助方法。
public static <T> UnaryOperator<StreamEx<T>> skipLast(int n)
{
return(stream -> skipLast(stream, n));
}
private static StreamEx<T> skipLast(Stream<T> input, int n)
{
// implement the real logic of skipLast
}
有了上面的内容,现在可以写...
StreamEx.
of(input).
chain(skipLast(10)).
forEach(System.out::println);
在使用 Java 8 Streams, I some times find that Stream doesn't have a particular method that I desire (e.g. takeWhile(), dropWhile(), skipLast()
). How do I create my own stream class which has the additional methods with out re-writing the entire Java 8 Stream 架构时?
我知道 StreamEx library and know that it has takeWhile() and dropWhile(). As of writing this, it doesn't have skipLast()
. I have filed an issue 这个方法。
自版本 0.5.4 StreamEx library has a chain()
方法。这允许创建可以轻松插入的辅助方法。
public static <T> UnaryOperator<StreamEx<T>> skipLast(int n)
{
return(stream -> skipLast(stream, n));
}
private static StreamEx<T> skipLast(Stream<T> input, int n)
{
// implement the real logic of skipLast
}
有了上面的内容,现在可以写...
StreamEx.
of(input).
chain(skipLast(10)).
forEach(System.out::println);