Protonpackの概要
1. 概要
このチュートリアルでは、いくつかの補完的な機能を追加することで標準のStream APIを拡張するライブラリである、Protonpackの主な機能について説明します。
JavaStream APIの基本については、this write-up hereを参照してください。
2. メーベン依存
Protonpackライブラリを使用するには、pom.xmlファイルに依存関係を追加する必要があります。
com.codepoetics
protonpack
1.15
Maven Centralで最新バージョンを確認してください。
3. StreamUtils
これは、Javaの標準StreamAPIを拡張するメインクラスです。
ここで説明するすべてのメソッドはintermediate operationsです。つまり、Streamを変更しますが、その処理はトリガーされません。
3.1. takeWhile()およびtakeUntil()
takeWhile()は、ソースストリームas long as they meet the supplied conditionから値を取得します。
Stream streamOfInt = Stream
.iterate(1, i -> i + 1);
List result = StreamUtils
.takeWhile(streamOfInt, i -> i < 5)
.collect(Collectors.toList());
assertThat(result).contains(1, 2, 3, 4);
逆に、takeUntil()は値until a value meets supplied conditionを取り、停止します。
Stream streamOfInt = Stream
.iterate(1, i -> i + 1);
List result = StreamUtils
.takeUntil(streamOfInt, i -> i >= 5)
.collect(Collectors.toList());
assertThat(result).containsExactly(1, 2, 3, 4);
Java 9以降では、takeWhile()は標準のStream APIの一部です。
3.2. zip()
zip()は、2つまたは3つのストリームを入力およびコンバイナー関数として受け取ります。 メソッドtakes a value from the same position of each stream and passes it to the combiner。
ストリームの1つが値を使い果たすまで、そうします。
String[] clubs = {"Juventus", "Barcelona", "Liverpool", "PSG"};
String[] players = {"Ronaldo", "Messi", "Salah"};
Set zippedFrom2Sources = StreamUtils
.zip(stream(clubs), stream(players), (club, player) -> club + " " + player)
.collect(Collectors.toSet());
assertThat(zippedFrom2Sources)
.contains("Juventus Ronaldo", "Barcelona Messi", "Liverpool Salah");
同様に、3つのソースストリームを受け取るオーバーロードされたzip():
String[] leagues = { "Serie A", "La Liga", "Premier League" };
Set zippedFrom3Sources = StreamUtils
.zip(stream(clubs), stream(players), stream(leagues),
(club, player, league) -> club + " " + player + " " + league)
.collect(Collectors.toSet());
assertThat(zippedFrom3Sources).contains(
"Juventus Ronaldo Serie A",
"Barcelona Messi La Liga",
"Liverpool Salah Premier League");
3.3. zipWithIndex()
zipWithIndex() 値を取得し、各値をそのインデックスでzipして、インデックス付きの値のストリームを作成します。
Stream streamOfClubs = Stream
.of("Juventus", "Barcelona", "Liverpool");
Set> zipsWithIndex = StreamUtils
.zipWithIndex(streamOfClubs)
.collect(Collectors.toSet());
assertThat(zipsWithIndex)
.contains(Indexed.index(0, "Juventus"), Indexed.index(1, "Barcelona"),
Indexed.index(2, "Liverpool"));
3.4. merge()
merge()は、複数のソースストリームとコンバイナーで機能します。 takes the value of the same index position from each source stream and passes it to the combinerです。
このメソッドは、seed値から開始して、各ストリームの同じインデックスから1つの値を連続して取得することで機能します。
次に、値がコンバイナに渡され、結果の結合値がコンバイナにフィードバックされて、次の値が作成されます。
Stream streamOfClubs = Stream
.of("Juventus", "Barcelona", "Liverpool", "PSG");
Stream streamOfPlayers = Stream
.of("Ronaldo", "Messi", "Salah");
Stream streamOfLeagues = Stream
.of("Serie A", "La Liga", "Premier League");
Set merged = StreamUtils.merge(
() -> "",
(valOne, valTwo) -> valOne + " " + valTwo,
streamOfClubs,
streamOfPlayers,
streamOfLeagues)
.collect(Collectors.toSet());
assertThat(merged)
.contains("Juventus Ronaldo Serie A", "Barcelona Messi La Liga",
"Liverpool Salah Premier League", "PSG");
3.5. mergeToList()
mergeToList()は、入力として複数のストリームを受け取ります。 combines the value of the same index from each stream into a List:
Stream streamOfClubs = Stream
.of("Juventus", "Barcelona", "PSG");
Stream streamOfPlayers = Stream
.of("Ronaldo", "Messi");
Stream> mergedStreamOfList = StreamUtils
.mergeToList(streamOfClubs, streamOfPlayers);
List> mergedListOfList = mergedStreamOfList
.collect(Collectors.toList());
assertThat(mergedListOfList.get(0))
.containsExactly("Juventus", "Ronaldo");
assertThat(mergedListOfList.get(1))
.containsExactly("Barcelona", "Messi");
assertThat(mergedListOfList.get(2))
.containsExactly("PSG");
3.6. interleave()
interleave()creates alternates values taken from multiple streams using a selector。
このメソッドは、各ストリームからselectorに1つの値を含むセットを提供し、selectorは1つの値を選択します。
次に、選択した値がセットから削除され、選択した値の元となった次の値に置き換えられます。 この反復は、すべてのソースの値がなくなるまで続きます。
次の例では、interleave() を使用して、round-robin戦略で交互の値を作成します。
Stream streamOfClubs = Stream
.of("Juventus", "Barcelona", "Liverpool");
Stream streamOfPlayers = Stream
.of("Ronaldo", "Messi");
Stream streamOfLeagues = Stream
.of("Serie A", "La Liga");
List interleavedList = StreamUtils
.interleave(Selectors.roundRobin(), streamOfClubs, streamOfPlayers, streamOfLeagues)
.collect(Collectors.toList());
assertThat(interleavedList)
.hasSize(7)
.containsExactly("Juventus", "Ronaldo", "Serie A", "Barcelona", "Messi", "La Liga", "Liverpool");
ラウンドロビンselector isはライブラリによってSelectors.roundRobin()として提供されるため、上記のコードはチュートリアル用であることに注意してください。
3.7. skipUntil() およびskipWhile()
skipUntil()は値until a value meets the conditionをスキップします:
Integer[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
List skippedUntilGreaterThan5 = StreamUtils
.skipUntil(stream(numbers), i -> i > 5)
.collect(Collectors.toList());
assertThat(skippedUntilGreaterThan5).containsExactly(6, 7, 8, 9, 10);
対照的に、skipWhile()skips the values while the values meet the condition:
Integer[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
List skippedWhileLessThanEquals5 = StreamUtils
.skipWhile(stream(numbers), i -> i <= 5 || )
.collect(Collectors.toList());
assertThat(skippedWhileLessThanEquals5).containsExactly(6, 7, 8, 9, 10);
skipWhile() についての重要なことの1つは、条件を満たさない最初の値が見つかった後もストリーミングを継続することです。
List skippedWhileGreaterThan5 = StreamUtils
.skipWhile(stream(numbers), i -> i > 5)
.collect(Collectors.toList());
assertThat(skippedWhileGreaterThan5).containsExactly(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Java 9以降では、標準のStream APIのdropWhile()は、skipWhile()と同じ機能を提供します。
3.8. unfold()
unfold()は、カスタムジェネレーターをシード値に適用し、次に生成された各値に適用することで、潜在的に無限のストリームを生成します。ストリームは、Optional.empty():を返すことで終了できます。
Stream unfolded = StreamUtils
.unfold(2, i -> (i < 100)
? Optional.of(i * i) : Optional.empty());
assertThat(unfolded.collect(Collectors.toList()))
.containsExactly(2, 4, 16, 256);
3.9. windowed()
windowed()creates multiple subsets of source stream as a stream of *List*. このメソッドは、ソースストリーム、window sizeおよびskip valueをパラメーターとして受け取ります。
Listの長さはwindowsize, に等しく、skip valueは、前のサブセットに対してサブセットが開始する場所を決定します。
Integer[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8 };
List windowedWithSkip1 = StreamUtils
.windowed(stream(numbers), 3, 1)
.collect(Collectors.toList());
assertThat(windowedWithSkip1)
.containsExactly(asList(1, 2, 3), asList(2, 3, 4), asList(3, 4, 5), asList(4, 5, 6), asList(5, 6, 7));
また、次の例に示すように、最後のウィンドウは目的のサイズであることが保証されています。
List windowedWithSkip2 = StreamUtils.windowed(stream(numbers), 3, 2).collect(Collectors.toList());
assertThat(windowedWithSkip2).containsExactly(asList(1, 2, 3), asList(3, 4, 5), asList(5, 6, 7));
3.10. aggregate()
まったく異なる動作をする2つのaggregate() メソッドがあります。
最初のaggregate() groups together elements of equal value according to a given predicate:
Integer[] numbers = { 1, 2, 2, 3, 4, 4, 4, 5 };
List aggregated = StreamUtils
.aggregate(Arrays.stream(numbers), (int1, int2) -> int1.compareTo(int2) == 0)
.collect(Collectors.toList());
assertThat(aggregated).containsExactly(asList(1), asList(2, 2), asList(3), asList(4, 4, 4), asList(5));
述部は値を連続して受け取ります。 したがって、番号が順序付けされていない場合、上記の結果は異なります。
一方、2番目のaggregate()は、単にgroup together elements from the source stream into groups of the desired sizeに使用されます。
List aggregatedFixSize = StreamUtils
.aggregate(stream(numbers), 5)
.collect(Collectors.toList());
assertThat(aggregatedFixSize).containsExactly(asList(1, 2, 2, 3, 4), asList(4, 4, 5));
3.11. aggregateOnListCondition()
aggregateOnListCondition()は、値based on predicate and current active groupをグループ化します。 述語には、現在アクティブなグループがListと次の値として与えられます。 次に、グループを続行するか、新しいグループを開始するかを決定する必要があります。
次の例は、隣接する整数値をグループにグループ化する要件を解決します。各グループの値の合計は5以下でなければなりません。
Integer[] numbers = { 1, 1, 2, 3, 4, 4, 5 };
Stream> aggregated = StreamUtils
.aggregateOnListCondition(stream(numbers),
(currentList, nextInt) -> currentList.stream().mapToInt(Integer::intValue).sum() + nextInt <= 5);
assertThat(aggregated)
.containsExactly(asList(1, 1, 2), asList(3), asList(4), asList(4), asList(5));
4. Streamable<T>
Stream のインスタンスは再利用できません。 このため、Streamable provides reusable streams by wrapping and exposing the same methods as the Stream:
Streamable s = Streamable.of("a", "b", "c", "d");
List collected1 = s.collect(Collectors.toList());
List collected2 = s.collect(Collectors.toList());
assertThat(collected1).hasSize(4);
assertThat(collected2).hasSize(4);
5. CollectorUtils
CollectorUtilsは、いくつかの便利なコレクターメソッドを追加することにより、標準のCollectorsを補完します。
5.1. maxBy() およびminBy()
maxBy()finds the maximum value in a stream using supplied projection logic:
Stream clubs = Stream.of("Juventus", "Barcelona", "PSG");
Optional longestName = clubs.collect(CollectorUtils.maxBy(String::length));
assertThat(longestName).contains("Barcelona");
対照的に、minBy()finds the minimum value using the supplied projection logic。
5.2. unique()
unique()コレクターは非常に単純なことを行います:it returns the only value if a given stream has exactly 1 element:
Stream singleElement = Stream.of(1);
Optional unique = singleElement.collect(CollectorUtils.unique());
assertThat(unique).contains(1);
それ以外の場合、unique()は例外をスローします。
Stream multipleElement = Stream.of(1, 2, 3);
assertThatExceptionOfType(NonUniqueValueException.class).isThrownBy(() -> {
multipleElement.collect(CollectorUtils.unique());
});
6. 結論
この記事では、ProtonpackライブラリーがJava Stream APIを拡張して使いやすくする方法を学びました。 一般的に使用される可能性のある便利なメソッドが追加されますが、標準APIにはありません。
Java 9以降、Protonpackが提供する機能の一部は標準のStream APIで利用可能になります。
いつものように、コードはover on Githubで見つけることができます。