public interface IntStream extends BaseStream<Integer,IntStream>
intに特化したStreamである。 次の例はStreamとIntStreamを使った集計処理を示し、赤いウィジェットの重さの和を計算する。
int sum = widgets.stream()
.filter(w -> w.getColor() == RED)
.mapToInt(w -> w.getWeight())
.sum();
更なるストリームの仕様・処理・ストリームパイプライン・並列性についてはStreamのクラスドキュメントとjava.util.streamのパッケージドキュメントを参照せよ。
| 修飾子とタイプ | インタフェースと説明 |
|---|---|
static interface |
IntStream.Builder
IntStreamの可変なビルダ StreamBuilderはライフサイクルを持ち、要素を追加できる構築中段階から始まり、要素を追加できなくなる構築済段階に移行する。 |
| 修飾子とタイプ | メソッドと説明 |
|---|---|
boolean |
allMatch(IntPredicate predicate)
このストリームの全ての要素が与えられた述語に適合するか返す。
|
boolean |
anyMatch(IntPredicate predicate)
このストリームのある要素が与えられた述語に適合するか返す。
|
DoubleStream |
asDoubleStream()
このストリームの要素を
doubleに変換した要素からなるDoubleStreamを返す。 |
LongStream |
asLongStream()
このストリームの要素を
longに変換した要素からなるLongStreamを返す。 |
OptionalDouble |
average()
|
Stream<Integer> |
boxed()
このストリームの要素を
Integerにボックス化した要素からなるStreamを返す。 |
static IntStream.Builder |
builder()
IntStreamのビルダを返す。 |
<R> R |
collect(Supplier<R> supplier,
ObjIntConsumer<R> accumulator,
BiConsumer<R,R> combiner)
このストリームの要素に可変的簡約を実行する。
|
static IntStream |
concat(IntStream a,
IntStream b)
ストリームの要素が、最初のストリームの全ての要素の後に2つ目のストリームの全ての要素を並べたような、遅延的に連結されたストリームを返す。
|
long |
count()
このストリームの要素数を返す。
|
IntStream |
distinct()
このストリームの要素のうち重複を除いた要素からなるストリームを返す。
|
static IntStream |
empty()
空の逐次的
IntStreamを返す。 |
IntStream |
filter(IntPredicate predicate)
このストリームの要素のうち、与えられた述語に適合する要素からなるストリームを返す。
|
OptionalInt |
findAny()
このストリームのある要素を表す
OptionalInt、もしくはストリームが空であれば空のOptionalIntを返す。 |
OptionalInt |
findFirst()
このストリームの最初の要素を表す
OptionalInt、もしくはストリームが空であれば空のOptionalIntを返す。 |
IntStream |
flatMap(IntFunction<? extends IntStream> mapper)
与えられた写像関数をこのストリームの各要素に適用して生成したストリームの内容で各要素を置き換えた結果からなるストリームを返す。
|
void |
forEach(IntConsumer action)
このストリームの各要素にアクションを適用する。
|
void |
forEachOrdered(IntConsumer action)
このストリームの各要素にアクションを適用する。
|
static IntStream |
generate(IntSupplier s)
各要素が
IntSupplierによって生成される、逐次的な無限ストリームを返す。 |
static IntStream |
iterate(int seed,
IntUnaryOperator f)
初期要素
seedに対する関数fの繰り返しの適用によって生成された無限IntStreamを返す。 |
PrimitiveIterator.OfInt |
iterator()
このストリームの要素のイテレータを返す。
|
IntStream |
limit(long maxSize)
このストリームの要素からなり、長さが
maxSizeより長くならないように切り詰められたストリームを返す。 |
IntStream |
map(IntUnaryOperator mapper)
このストリームの要素に与えられた関数を適用した結果からなるストリームを返す。
|
DoubleStream |
mapToDouble(IntToDoubleFunction mapper)
このストリームの要素に与えられた関数を適用した結果からなる
DoubleStreamを返す。 |
LongStream |
mapToLong(IntToLongFunction mapper)
このストリームの要素に与えられた関数を適用した結果からなる
LongStreamを返す。 |
<U> Stream<U> |
mapToObj(IntFunction<? extends U> mapper)
このストリームの要素に与えられた関数を適用した結果からなる、オブジェクトを値として持つ
Streamを返す。 |
OptionalInt |
max()
|
OptionalInt |
min()
|
boolean |
noneMatch(IntPredicate predicate)
このストリームのどの要素も与えられた述語に適合しないか返す。
|
static IntStream |
of(int... values)
要素が指定された値であるような逐次的なストリームを返す。
|
static IntStream |
of(int t)
1つの要素を含む逐次的な
IntStreamを返す。 |
IntStream |
parallel()
並列であり同等なストリームを返す。
|
IntStream |
peek(IntConsumer action)
このストリームの要素からなり、加えて結果のストリームから要素が消費されるごとにその要素にアクションを実行するストリームを返す。
|
static IntStream |
range(int startInclusive,
int endExclusive)
startInclusive(この値を含む)からendExclusive(この値を含まない)まで、1ずつ増加する順序を持つ逐次的なIntStreamを返す。 |
static IntStream |
rangeClosed(int startInclusive,
int endInclusive)
startInclusive(この値を含む)からendInclusive(この値を含む)まで、1ずつ増加する順序を持つ逐次的なIntStreamを返す。 |
OptionalInt |
reduce(IntBinaryOperator op)
|
int |
reduce(int identity,
IntBinaryOperator op)
|
IntStream |
sequential()
逐次的であり同等なストリームを返す。
|
IntStream |
skip(long n)
このストリームの
n個の要素を取り除いた残りの要素からなるストリームを返す。 |
IntStream |
sorted()
このストリームの要素を、整列した結果からなるストリームを返す。
|
Spliterator.OfInt |
spliterator()
このストリームの要素のスプリッテレータを返す。
|
int |
sum()
このストリームの要素の和を返す。
|
IntSummaryStatistics |
summaryStatistics()
このストリームの要素の各種概要情報を表す
IntSummaryStatisticsを返す。 |
int[] |
toArray()
このストリームの要素からなる配列を返す。
|
close, isParallel, onClose, unorderedIntStream filter(IntPredicate predicate)
これは中間処理である。
<U> Stream<U> mapToObj(IntFunction<? extends U> mapper)
Streamを返す。 これは中間処理である。
LongStream mapToLong(IntToLongFunction mapper)
LongStreamを返す。 これは中間処理である。
DoubleStream mapToDouble(IntToDoubleFunction mapper)
DoubleStreamを返す。 これは中間処理である。
IntStream peek(IntConsumer action)
これは中間処理である。
並列ストリームパイプラインの場合、アクションは上流の処理から利用可能になった際に任意の時間と任意のスレッドで呼ばれる。もしアクションが共有状態を変更した場合、必要な同期を用意する必要がある。
IntStream limit(long maxSize)
maxSizeより長くならないように切り詰められたストリームを返す。 これは短絡的で状態を持つ中間処理である。
IntStream skip(long n)
void forEach(IntConsumer action)
これは末端処理である。
並列ストリームパイプラインの場合、この処理はストリームの出現順順序を尊重するとは限らない。そのようにしてしまうと並列処理の利点を犠牲にしてしまうためである。与えられた要素に対して、アクションはライブラリが選んだ任意の時間と任意のスレッドで実行される。もしアクションが共有状態を変更するならば、アクションは必要な同期処理を用意する責任を負う。
void forEachOrdered(IntConsumer action)
これは末端処理である。
int[] toArray()
これは末端処理である。
int reduce(int identity,
IntBinaryOperator op)
int result = identity;
for (int element : this stream)
result = accumulator.applyAsInt(result, element)
return result;
ただし逐次的に実行されるとは制約されていない。 値identityは累積関数の単位元である必要がある。つまり、任意のxに対してaccumulator.apply(identity, x)はxと等しい。accumulator関数は結合的関数である必要がある。
これは末端処理である。
OptionalInt reduce(IntBinaryOperator op)
OptionalIntを返す。これは次と等しい。
boolean foundAny = false;
int result = null;
for (int element : this stream) {
if (!foundAny) {
foundAny = true;
result = element;
}
else
result = accumulator.applyAsInt(result, element);
}
return foundAny ?OptionalInt.of(result) : OptionalInt.empty();
ただし逐次的に実行されるとは制約されていない。 accumulator関数は結合的関数である必要がある。
これは末端処理である。
<R> R collect(Supplier<R> supplier,
ObjIntConsumer<R> accumulator,
BiConsumer<R,R> combiner)
ArrayListなどの可変な結果コンテナであるものであり、結果を置き換えるのではなく結果の状態を変更して各要素を組み入れるような簡約である。これは次のコードと同じ結果を生成する。
R result = supplier.get();
for (int element : this stream)
accumulator.accept(result, element);
return result;
reduce(int, IntBinaryOperator)のように、collect処理は追加の同期処理を必要とせずに並列化できる。
これは末端処理である。
int sum()
OptionalInt min()
OptionalInt max()
long count()
OptionalDouble average()
IntSummaryStatistics summaryStatistics()
boolean anyMatch(IntPredicate predicate)
falseを返す。 これは短絡的で状態を持つ末端処理である。
boolean allMatch(IntPredicate predicate)
trueを返す。 これは短絡的で状態を持つ末端処理である。
boolean noneMatch(IntPredicate predicate)
trueを返す。 これは短絡的で状態を持つ末端処理である。
OptionalInt findFirst()
OptionalInt、もしくはストリームが空であれば空のOptionalIntを返す。このストリームが出現順順序を持たなければ任意の要素が返される場合がある。 これは短絡的で状態を持つ末端処理である。
OptionalInt findAny()
OptionalInt、もしくはストリームが空であれば空のOptionalIntを返す。 これは短絡的で状態を持つ末端処理である。
この処理の動作は明示的に非決定的であり、どの要素を選んでもよい。これにより並列実行時の性能を最大化できる。その際のコストは同じ情報源に対する複数回の呼び出しが同じ値を返さないことである(もし安定した結果を望むならば、代わりにfindFirst()を用いよ)。
LongStream asLongStream()
DoubleStream asDoubleStream()
Stream<Integer> boxed()
IntStream sequential()
BaseStreamこれは中間処理である。
sequential インタフェース内 BaseStream<Integer,IntStream>IntStream parallel()
BaseStreamこれは中間処理である。
parallel インタフェース内 BaseStream<Integer,IntStream>PrimitiveIterator.OfInt iterator()
BaseStreamこれは末端処理である。
iterator インタフェース内 BaseStream<Integer,IntStream>Spliterator.OfInt spliterator()
BaseStreamこれは末端処理である。
spliterator インタフェース内 BaseStream<Integer,IntStream>static IntStream.Builder builder()
IntStreamのビルダを返す。
static IntStream empty()
IntStreamを返す。
static IntStream of(int t)
IntStreamを返す。
static IntStream of(int... values)
static IntStream iterate(int seed, IntUnaryOperator f)
seedに対する関数fの繰り返しの適用によって生成された無限IntStreamを返す。seed, f(seed), f(f(seed))などからなるLongStreamを生成する。 IntStreamの最初の要素(位置0)はseedによって与えられる。n > 0に対しては、位置nの要素はfを位置n - 1の要素に適用した結果である。
static IntStream generate(IntSupplier s)
IntSupplierによって生成される、逐次的な無限ストリームを返す。定数のストリームや乱数のストリームなどを生成するのに向いている。
static IntStream range(int startInclusive, int endExclusive)
startInclusive(この値を含む)からendExclusive(この値を含まない)まで、1ずつ増加する順序を持つ逐次的なIntStreamを返す。
static IntStream rangeClosed(int startInclusive, int endInclusive)
startInclusive(この値を含む)からendInclusive(この値を含む)まで、1ずつ増加する順序を持つ逐次的なIntStreamを返す。