あなたはEclipse Collections(旧GSコレクション)を使用している場合、あなたはすべてのRichIterables
にpartition
メソッドを使用することができます。
MutableList<Integer> integers = FastList.newListWith(-3, -2, -1, 0, 1, 2, 3);
PartitionMutableList<Integer> result = integers.partition(IntegerPredicates.isEven());
Assert.assertEquals(FastList.newListWith(-2, 0, 2), result.getSelected());
Assert.assertEquals(FastList.newListWith(-3, -1, 1, 3), result.getRejected());
カスタム型、PartitionMutableList
を使用する理由は、代わりのPair
がgetSelected()とgetRejectedための共変戻り型を可能にすることです()。たとえば、MutableCollection
をパーティショニングすると、リストではなく2つのコレクションが得られます。
MutableCollection<Integer> integers = ...;
PartitionMutableCollection<Integer> result = integers.partition(IntegerPredicates.isEven());
MutableCollection<Integer> selected = result.getSelected();
コレクションがRichIterable
でない場合は、引き続きEclipse Collectionsで静的ユーティリティを使用できます。
PartitionIterable<Integer> partitionIterable = Iterate.partition(integers, IntegerPredicates.isEven());
PartitionMutableList<Integer> partitionList = ListIterate.partition(integers, IntegerPredicates.isEven());
注:私はEclipseのコレクションのためのコミッターです。新しいJava 8つの特徴(streamとlambda epressions)で
注する場合のある別の双方GC向け及びカプセル化アプローチは、元のコレクションの周囲のJava 8フィルタリングラッパー・ストリームを使用している
事前に設定されたパート・シヨン・キーの限定されたセットを使用して、各繰り返しですべての異なるキー・アイテムをスキップする各パーティション・キーごとにコレクションをもう一度反復するだけでGCにはるかに効率的です。 – Vadzim
GCに優しいアプローチとカプセル化されたアプローチのどちらも、元のコレクションの周囲にJava 8フィルタリングラッパーストリームを使用しています。https://stackoverflow.com/questions/19940319/can-you-split-a-stream-into-two-streams – Vadzim