Java : IntStream - API使用例
IntStream (Java SE 21 & JDK 21) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様書のおともにどうぞ。
概要
IntStream クラスは、プリミティブ型 の int を扱う Stream です。
IntStream には、生成に便利な range、rangeClosed メソッドがあります。
for文の代わりにも使えます。
ストリームの基本的なことは下記の記事で解説していますので、そちらもご参照ください。
final var stream = IntStream.range(0, 5);
System.out.println("-- forEach --");
stream.forEach(v -> {
System.out.println(v);
});
// 結果
// ↓
//-- forEach --
//0
//1
//2
//3
//4
final var stream = IntStream.of(10, 20, 30);
final var ret = stream.toArray();
System.out.println(Arrays.toString(ret)); // [10, 20, 30]
メソッド
boolean allMatch (IntPredicate predicate)
final var predicate = new IntPredicate() {
@Override
public boolean test(int value) {
// 偶数か判定します。
return value % 2 == 0;
}
};
{
final var stream = IntStream.of(0, 2, 4);
final var ret = stream.allMatch(predicate);
System.out.println(ret); // true
}
{
final var stream = IntStream.of(0, 1, 2, 3, 4);
final var ret = stream.allMatch(predicate);
System.out.println(ret); // false
}
boolean anyMatch (IntPredicate predicate)
final var predicate = new IntPredicate() {
@Override
public boolean test(int value) {
// 偶数か判定します。
return value % 2 == 0;
}
};
{
final var stream = IntStream.of(0, 2, 4);
final var ret = stream.anyMatch(predicate);
System.out.println(ret); // true
}
{
final var stream = IntStream.of(1, 2, 3);
final var ret = stream.anyMatch(predicate);
System.out.println(ret); // true
}
{
final var stream = IntStream.of(1, 3, 5);
final var ret = stream.anyMatch(predicate);
System.out.println(ret); // false
}
DoubleStream asDoubleStream ()
final var stream = IntStream.of(1, 2, 3);
final var doubleStream = stream.asDoubleStream().map(v -> v / 2.0);
final var ret = doubleStream.toArray();
System.out.println(Arrays.toString(ret)); // [0.5, 1.0, 1.5]
LongStream asLongStream ()
final var stream = IntStream.of(1, 2, 3);
final var longStream = stream.asLongStream().map(v -> v * 10000000000L);
final var ret = longStream.toArray();
// [10000000000, 20000000000, 30000000000]
System.out.println(Arrays.toString(ret));
OptionalDouble average ()
final var stream = IntStream.of(1, 2, 3, 4, 5, 6);
final var opt = stream.average();
opt.ifPresent(value -> {
System.out.println(value); // 3.5
});
final var opt = IntStream.empty().average();
System.out.println(opt.isEmpty()); // true
Stream<Integer> boxed ()
final Stream<Integer> stream = IntStream.range(0, 5).boxed();
final var ret = stream.toList();
System.out.println(ret); // [0, 1, 2, 3, 4]
static IntStream.Builder builder ()
final var builder = IntStream.builder();
builder.add(0).add(2).add(4);
final var stream = builder.build();
final var ret = stream.toArray();
System.out.println(Arrays.toString(ret)); // [0, 2, 4]
<R> R collect (Supplier<R> supplier, ObjIntConsumer<R> accumulator, BiConsumer<R,R> combiner)
final var supplier = new Supplier<List<String>>() {
@Override
public List<String> get() {
System.out.println("-- supplier --");
return new ArrayList<>();
}
};
final var accumulator = new ObjIntConsumer<List<String>>() {
@Override
public void accept(List<String> list, int value) {
System.out.println("-- accumulator --");
System.out.println(" list = " + list);
System.out.println(" value = " + value);
list.add("%#x".formatted(value));
}
};
final var combiner = new BiConsumer<List<String>, List<String>>() {
@Override
public void accept(List<String> list1, List<String> list2) {
System.out.println("-- combiner --");
System.out.println(" list1 = " + list1);
System.out.println(" list2 = " + list2);
list1.addAll(list2);
}
};
{
final var stream = IntStream.of(1, 100, 255);
final var ret = stream.collect(supplier, accumulator, combiner);
System.out.println("collect = " + ret);
// 結果
// ↓
//-- supplier --
//-- accumulator --
// list = []
// value = 1
//-- accumulator --
// list = [0x1]
// value = 100
//-- accumulator --
// list = [0x1, 0x64]
// value = 255
//collect = [0x1, 0x64, 0xff]
}
{
final var parallelStream = IntStream.of(1, 100, 255).parallel();
System.out.println(parallelStream.isParallel()); // true
final var ret = parallelStream.collect(supplier, accumulator, combiner);
System.out.println("collect = " + ret);
// 結果
// ↓
//-- supplier --
//-- accumulator --
// list = []
// value = 100
//-- supplier --
//-- accumulator --
// list = []
// value = 255
//-- supplier --
//-- accumulator --
// list = []
// value = 1
//-- combiner --
// list1 = [0x64]
// list2 = [0xff]
//-- combiner --
// list1 = [0x1]
// list2 = [0x64, 0xff]
//collect = [0x1, 0x64, 0xff]
}
static IntStream concat (IntStream a, IntStream b)
final var stream1 = IntStream.of(1, 2, 3);
final var stream2 = IntStream.of(100, 200, 300);
System.out.println(stream1.isParallel()); // false
System.out.println(stream2.isParallel()); // false
final var concatStream = IntStream.concat(stream1, stream2);
System.out.println(concatStream.isParallel()); // false
final var ret = concatStream.toArray();
System.out.println(Arrays.toString(ret)); // [1, 2, 3, 100, 200, 300]
final var stream1 = IntStream.of(1, 2, 3).parallel();
final var stream2 = IntStream.of(100, 200, 300).parallel();
System.out.println(stream1.isParallel()); // true
System.out.println(stream2.isParallel()); // true
final var concatStream = IntStream.concat(stream1, stream2);
System.out.println(concatStream.isParallel()); // true
final var ret = concatStream.toArray();
System.out.println(Arrays.toString(ret)); // [1, 2, 3, 100, 200, 300]
long count ()
final var stream = IntStream.of(0, 2, 4, 6);
System.out.println(stream.count()); // 4
final var stream = IntStream.empty();
System.out.println(stream.count()); // 0
IntStream distinct ()
final var stream = IntStream.of(1, 2, 2, 3, 3, 3);
final var ret = stream.distinct().toArray();
System.out.println(Arrays.toString(ret)); // [1, 2, 3]
default IntStream dropWhile (IntPredicate predicate)
final var stream = IntStream.of(1, 2, 3, 4, 5);
final var predicate = new IntPredicate() {
@Override
public boolean test(int value) {
return value != 3;
}
};
final var ret = stream.dropWhile(predicate).toArray();
System.out.println(Arrays.toString(ret)); // [3, 4, 5]
final var stream = IntStream.of(-1, -2, -3, -4, -5);
final var ret = stream.dropWhile(v -> v != -4).toArray();
System.out.println(Arrays.toString(ret)); // [-4, -5]
static IntStream empty ()
final var stream = IntStream.empty();
System.out.println(stream.count()); // 0
final var stream = IntStream.empty();
final var array = stream.toArray();
System.out.println(Arrays.toString(array)); // []
IntStream filter (IntPredicate predicate)
final var stream = IntStream.of(0, 1, 2, 3, 4, 5);
final var predicate = new IntPredicate() {
@Override
public boolean test(int value) {
// 偶数か判定します。
return value % 2 == 0;
}
};
final var ret = stream.filter(predicate).toArray();
System.out.println(Arrays.toString(ret)); // [0, 2, 4]
final var stream = IntStream.of(0, 1, 2, 3, 4, 5);
final var ret = stream.filter(v -> v % 2 == 1).toArray();
System.out.println(Arrays.toString(ret)); // [1, 3, 5]
OptionalInt findAny ()
int count1 = 0;
int count2 = 0;
int count3 = 0;
for (int i = 0; i < 1000; i++) {
final var stream = IntStream.of(1, 2, 3).parallel();
System.out.println(stream.isParallel()); // true
// 1 or 2 or 3
final var opt = stream.findAny();
if (opt.isEmpty()) {
throw new IllegalStateException();
}
switch (opt.orElseThrow()) {
case 1 -> count1++;
case 2 -> count2++;
case 3 -> count3++;
}
}
System.out.println("count1 : " + count1);
System.out.println("count2 : " + count2);
System.out.println("count3 : " + count3);
// 結果
// ↓
//count1 : 183
//count2 : 814
//count3 : 3
final var stream = IntStream.empty();
final var opt = stream.findAny();
System.out.println(opt.isEmpty()); // true
OptionalInt findFirst ()
final var stream = IntStream.of(1, 2, 3);
final var opt = stream.findFirst();
opt.ifPresent(value -> {
System.out.println(value); // 1
});
// parallel
final var stream = IntStream.of(1, 2, 3).parallel();
System.out.println(stream.isParallel()); // true
final var opt = stream.findFirst();
opt.ifPresent(value -> {
System.out.println(value); // 1
});
int count1 = 0;
int count2 = 0;
int count3 = 0;
for (int i = 0; i < 1000; i++) {
// unordered and parallel
final var stream = IntStream.of(1, 2, 3).unordered().parallel();
// 1 or 2 or 3
final var opt = stream.findFirst();
if (opt.isEmpty()) {
throw new IllegalStateException();
}
switch (opt.orElseThrow()) {
case 1 -> count1++;
case 2 -> count2++;
case 3 -> count3++;
}
}
System.out.println("count1 : " + count1);
System.out.println("count2 : " + count2);
System.out.println("count3 : " + count3);
// 結果
// ↓
//count1 : 137
//count2 : 856
//count3 : 7
final var stream = IntStream.empty();
final var opt = stream.findFirst();
System.out.println(opt.isEmpty()); // true
IntStream flatMap (IntFunction<? extends IntStream> mapper)
final var stream = IntStream.of(100, 200, 300);
final var mapper = new IntFunction<IntStream>() {
@Override
public IntStream apply(int value) {
return IntStream.of(value, value + 1, value + 2);
}
};
final var ret = stream.flatMap(mapper).toArray();
// [100, 101, 102, 200, 201, 202, 300, 301, 302]
System.out.println(Arrays.toString(ret));
void forEach (IntConsumer action)
関連 : forEachOrdered(IntConsumer action)
final var stream = IntStream.of(10, 20, 30);
final var action = new IntConsumer() {
@Override
public void accept(int value) {
System.out.println(value);
}
};
System.out.println("-- forEach --");
stream.forEach(action);
// 結果
// ↓
//-- forEach --
//10
//20
//30
// ラムダ式を使う例です。
final var stream = IntStream.of(-10, -20, -30);
System.out.println("-- forEach --");
stream.forEach(v -> {
System.out.println(v);
});
// 結果
// ↓
//-- forEach --
//-10
//-20
//-30
final var stream = IntStream.of(10, 20, 30, 40, 50).parallel();
System.out.println(stream.isParallel()); // true
System.out.println("-- forEach --");
stream.forEach(v -> {
System.out.println(v);
});
// 結果
// ↓
//-- forEach --
//30
//50
//40
//10
//20
void forEachOrdered (IntConsumer action)
final var stream = IntStream.of(10, 20, 30);
final var action = new IntConsumer() {
@Override
public void accept(int value) {
System.out.println(value);
}
};
System.out.println("-- forEach --");
stream.forEachOrdered(action);
// 結果
// ↓
//-- forEach --
//10
//20
//30
final var stream = IntStream.of(10, 20, 30, 40, 50).parallel();
System.out.println(stream.isParallel()); // true
System.out.println("-- forEach --");
stream.forEachOrdered(v -> {
System.out.println(v);
});
// 結果
// ↓
//-- forEach --
//10
//20
//30
//40
//50
static IntStream generate (IntSupplier s)
final var supplier = new IntSupplier() {
int count = 0;
@Override
public int getAsInt() {
count += 10;
return count;
}
};
final var stream = IntStream.generate(supplier);
final var ret = stream.limit(5).toArray();
System.out.println(Arrays.toString(ret)); // [10, 20, 30, 40, 50]
static IntStream iterate (int seed, IntPredicate hasNext, IntUnaryOperator next)
final var hasNext = new IntPredicate() {
@Override
public boolean test(int value) {
return value <= 100;
}
};
final var next = new IntUnaryOperator() {
@Override
public int applyAsInt(int operand) {
return operand * 2;
}
};
final var stream = IntStream.iterate(1, hasNext, next);
final var ret = stream.toArray();
System.out.println(Arrays.toString(ret)); // [1, 2, 4, 8, 16, 32, 64]
static IntStream iterate (int seed, IntUnaryOperator f)
final var next = new IntUnaryOperator() {
@Override
public int applyAsInt(int operand) {
return operand * 2;
}
};
final var stream = IntStream.iterate(1, next);
final var ret = stream.limit(10).toArray();
// [1, 2, 4, 8, 16, 32, 64, 128, 256, 512]
System.out.println(Arrays.toString(ret));
IntStream limit (long maxSize)
final var stream = IntStream.of(10, 20, 30, 40, 50, 60);
final var ret = stream.limit(3).toArray();
System.out.println(Arrays.toString(ret)); // [10, 20, 30]
IntStream map (IntUnaryOperator mapper)
final var stream = IntStream.of(1, 2, 3);
final var mapper = new IntUnaryOperator() {
@Override
public int applyAsInt(int operand) {
return operand * 2;
}
};
final var ret = stream.map(mapper).toArray();
System.out.println(Arrays.toString(ret)); // [2, 4, 6]
final var stream = IntStream.range(0, 4);
final var ret = stream.map(v -> v * 100).toArray();
System.out.println(Arrays.toString(ret)); // [0, 100, 200, 300]
default IntStream mapMulti (IntStream.IntMapMultiConsumer mapper)
final var stream = IntStream.of(100, 200, 300);
final var mapper = new IntStream.IntMapMultiConsumer() {
@Override
public void accept(int value, IntConsumer ic) {
ic.accept(value);
ic.accept(value + 1);
ic.accept(value + 2);
}
};
final var ret = stream.mapMulti(mapper).toArray();
// [100, 101, 102, 200, 201, 202, 300, 301, 302]
System.out.println(Arrays.toString(ret));
DoubleStream mapToDouble (IntToDoubleFunction mapper)
final var stream = IntStream.of(100, 125, 200);
final var mapper = new IntToDoubleFunction() {
@Override
public double applyAsDouble(int value) {
return value / 2.0;
}
};
final var ret = stream.mapToDouble(mapper).toArray();
System.out.println(Arrays.toString(ret)); // [50.0, 62.5, 100.0]
LongStream mapToLong (IntToLongFunction mapper)
final var stream = IntStream.of(1, 2, 3);
final var mapper = new IntToLongFunction() {
@Override
public long applyAsLong(int value) {
return value * 10000000000L;
}
};
final var ret = stream.mapToLong(mapper).toArray();
// [10000000000, 20000000000, 30000000000]
System.out.println(Arrays.toString(ret));
<U> Stream<U> mapToObj (IntFunction<? extends U> mapper)
final var stream = IntStream.of(1, 2, 3);
final var mapper = new IntFunction<String>() {
@Override
public String apply(int value) {
return "num=" + value;
}
};
final var ret = stream.mapToObj(mapper).toList();
System.out.println(ret); // [num=1, num=2, num=3]
OptionalInt max ()
final var stream = IntStream.of(10, 5, 100, 30);
final var opt = stream.max();
opt.ifPresent(value -> {
System.out.println(value); // 100
});
final var stream = IntStream.empty();
final var opt = stream.max();
System.out.println(opt.isEmpty()); // true
OptionalInt min ()
final var stream = IntStream.of(10, 5, 100, 30);
final var opt = stream.min();
opt.ifPresent(value -> {
System.out.println(value); // 5
});
final var stream = IntStream.empty();
final var opt = stream.min();
System.out.println(opt.isEmpty()); // true
boolean noneMatch (IntPredicate predicate)
final var predicate = new IntPredicate() {
@Override
public boolean test(int value) {
// 偶数か判定します。
return value % 2 == 0;
}
};
{
final var stream = IntStream.of(0, 2, 4);
final var ret = stream.noneMatch(predicate);
System.out.println(ret); // false
}
{
final var stream = IntStream.of(1, 2, 3);
final var ret = stream.noneMatch(predicate);
System.out.println(ret); // false
}
{
final var stream = IntStream.of(1, 3, 5);
final var ret = stream.noneMatch(predicate);
System.out.println(ret); // true
}
static IntStream of (int t)
final var stream = IntStream.of(0);
final var ret = stream.toArray();
System.out.println(Arrays.toString(ret)); // [0]
final var stream = IntStream.of(123);
final var ret = stream.toArray();
System.out.println(Arrays.toString(ret)); // [123]
static IntStream of (int... values)
final var stream = IntStream.of(0, 2, 4, 6, 8, 10);
final var ret = stream.toArray();
System.out.println(Arrays.toString(ret)); // [0, 2, 4, 6, 8, 10]
IntStream peek (IntConsumer action)
final var stream = IntStream.of(10, 20, 30);
final var action = new IntConsumer() {
@Override
public void accept(int value) {
System.out.println("peek : value = " + value);
}
};
final var array = stream.peek(action).toArray();
System.out.println("array = " + Arrays.toString(array));
// 結果
// ↓
//peek : value = 10
//peek : value = 20
//peek : value = 30
//array = [10, 20, 30]
static IntStream range (int startInclusive, int endExclusive)
final var stream = IntStream.range(0, 10);
final var ret = stream.toArray();
// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
System.out.println(Arrays.toString(ret));
final var stream = IntStream.range(-5, 5);
final var ret = stream.toArray();
// [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]
System.out.println(Arrays.toString(ret));
static IntStream rangeClosed (int startInclusive, int endInclusive)
final var stream = IntStream.rangeClosed(0, 10);
final var ret = stream.toArray();
// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
System.out.println(Arrays.toString(ret));
final var stream = IntStream.rangeClosed(-5, 5);
final var ret = stream.toArray();
// [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
System.out.println(Arrays.toString(ret));
int reduce (int identity, IntBinaryOperator op)
final var stream = IntStream.of(1, 2, 3, 4);
final var identity = 0;
final var op = new IntBinaryOperator() {
@Override
public int applyAsInt(int left, int right) {
if (identity == left) {
return right;
}
return left + right;
}
};
// 1 + 2 + 3 + 4 = 10
final var ret = stream.reduce(identity, op);
System.out.println(ret); // 10
final var stream = IntStream.empty();
final var identity = 0;
final var ret = stream.reduce(identity, (left, right) -> {
if (identity == left) {
return right;
}
return left + right;
});
System.out.println(ret); // 0
OptionalInt reduce (IntBinaryOperator op)
final var stream = IntStream.of(1, 2, 3, 4);
final var op = new IntBinaryOperator() {
@Override
public int applyAsInt(int left, int right) {
return left + right;
}
};
// 1 + 2 + 3 + 4 = 10
final var opt = stream.reduce(op);
opt.ifPresent(value -> {
System.out.println(value); // 10
});
final var stream = IntStream.empty();
final var opt = stream.reduce((left, right) -> left + right);
System.out.println(opt.isEmpty()); // true
IntStream skip (long n)
final var stream = IntStream.of(10, 20, 30, 40, 50, 60, 70);
final var ret = stream.skip(4).toArray();
System.out.println(Arrays.toString(ret)); // [50, 60, 70]
IntStream sorted ()
final var stream = IntStream.of(5, 3, 2, 4, 1);
final var ret = stream.sorted().toArray();
System.out.println(Arrays.toString(ret)); // [1, 2, 3, 4, 5]
final var stream = IntStream.of(5, 3, 2, 4, 1).parallel();
System.out.println(stream.isParallel()); // true
final var ret = stream.sorted().toArray();
System.out.println(Arrays.toString(ret)); // [1, 2, 3, 4, 5]
int sum ()
final var stream = IntStream.of(1, 2, 3, 4, 5);
System.out.println(stream.sum()); // 15
final var stream = IntStream.of(-10, -20, -30);
System.out.println(stream.sum()); // -60
IntSummaryStatistics summaryStatistics ()
final var stream = IntStream.of(1, 2, 3, 4, 5);
final var ret = stream.summaryStatistics();
// IntSummaryStatistics{count=5, sum=15, min=1, average=3.000000, max=5}
System.out.println(ret);
default IntStream takeWhile (IntPredicate predicate)
final var stream = IntStream.of(1, 2, 3, 4, 5);
final var predicate = new IntPredicate() {
@Override
public boolean test(int value) {
return value != 4;
}
};
final var ret = stream.takeWhile(predicate).toArray();
System.out.println(Arrays.toString(ret)); // [1, 2, 3]
final var stream = IntStream.of(-1, -2, -3, -4, -5);
final var ret = stream.takeWhile(v -> v != -3).toArray();
System.out.println(Arrays.toString(ret)); // [-1, -2]
int[] toArray ()
final var stream = IntStream.of(1, 2, 3, 4, 5);
final int[] ret = stream.toArray();
System.out.println(Arrays.toString(ret)); // [1, 2, 3, 4, 5]
BaseStreamで宣言されたメソッド
close, isParallel, iterator, onClose, parallel, sequential, spliterator, unordered
「Java API 使用例 : BaseStream」をご参照ください。