Lightweight Stream API
v1.2.2
Java 8 中的 Stream API 在 Java 7 及更低版本的迭代器上重寫。
Supplier
、 Function
、 Consumer
等);Stream
/ IntStream
/ LongStream
/ DoubleStream
(沒有並行處理,但具有各種附加方法和自訂運算子);Optional
/ OptionalBoolean
/ OptionalInt
/ OptionalLong
/ OptionalDouble
類別;Exceptional
類別-處理異常的函數式方式;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 ( new Reverse <>())
. forEach (...);
public final class Reverse < T > implements UnaryOperator < Stream < T >> {
@ Override
public Stream < T > apply ( Stream < T > stream ) {
final Iterator <? extends T > iterator = stream . getIterator ();
final ArrayDeque < T > deque = new ArrayDeque < T >();
while ( iterator . hasNext ()) {
deque . addFirst ( iterator . next ());
}
return Stream . of ( deque . iterator ());
}
}
您可以在這裡找到更多範例。
除了向後移植的 Java 8 Stream 運算子之外,該程式庫還提供:
filterNot
- 否定filter
運算符
// Java 8
stream . filter ((( Predicate < String >) String :: isEmpty ). negate ())
// LSA
stream . filterNot ( String :: isEmpty )
select
- 過濾給定類別的實例
// Java 8
stream . filter ( Integer . class :: isInstance )
// LSA
stream . select ( Integer . class )
withoutNulls
- 僅過濾非空元素
Stream . of ( "a" , null , "c" , "d" , null )
. withoutNulls () // [a, c, d]
sortBy
- 依擷取器函數排序
// Java 8
stream . sorted ( Comparator . comparing ( Person :: getName ))
// LSA
stream . sortBy ( Person :: getName )
groupBy
- 按擷取器函數分組
// Java 8
stream . collect ( Collectors . groupingBy ( Person :: getName )). entrySet (). stream ()
// LSA
stream . groupBy ( Person :: getName )
chunkBy
- 依分類器函數對排序流進行分區
Stream . of ( "a" , "b" , "cd" , "ef" , "gh" , "ij" , "klmnn" )
. chunkBy ( String :: length ) // [[a, b], [cd, ef, gh, ij], [klmnn]]
sample
- 發出每第 n 個元素
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
- 迭代應用累積函數並傳回 Stream
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 8
stream . map ( file -> {
try {
return new FileInputStream ( file );
} catch ( IOException ioe ) {
return null ;
}
})
// LSA
stream . map ( Function . Util . safe ( FileInputStream :: new ))
下載最新版本或透過 Maven 取得:
< dependency >
< groupId >com.annimon</ groupId >
< artifactId >stream</ artifactId >
< version >1.2.2</ version >
</ dependency >
或搖籃:
dependencies {
.. .
implementation ' com.annimon:stream:1.2.2 '
.. .
}
或使用 JitPack 的最新未發布功能。
還包括Java ME版本。查看 javame 分支。