Java : DoubleStream - API使用例
DoubleStream (Java SE 21 & JDK 21) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様書のおともにどうぞ。
概要
DoubleStream クラスは、プリミティブ型 の double を扱う Stream です。
ストリームの基本的なことは下記の記事で解説していますので、そちらもご参照ください。
final var stream = DoubleStream.of(0.1, 0.2, 0.3);
System.out.println("-- forEach --");
stream.forEach(v -> {
System.out.println(v);
});
// 結果
// ↓
//-- forEach --
//0.1
//0.2
//0.3
final var stream = DoubleStream.of(0.1, 0.2, 0.3);
final var ret = stream.map(v -> v * 2.0).toArray();
System.out.println(Arrays.toString(ret)); // [0.2, 0.4, 0.6]
メソッド
boolean allMatch (DoublePredicate predicate)
final var predicate = new DoublePredicate() {
@Override
public boolean test(double value) {
// 値がマイナスか判定します。
return value < 0.0;
}
};
{
final var stream = DoubleStream.of(-0.1, -0.2, -0.3);
final var ret = stream.allMatch(predicate);
System.out.println(ret); // true
}
{
final var stream = DoubleStream.of(1.0, -2.0, 3.0, -4.0);
final var ret = stream.allMatch(predicate);
System.out.println(ret); // false
}
boolean anyMatch (DoublePredicate predicate)
final var predicate = new DoublePredicate() {
@Override
public boolean test(double value) {
// 値がマイナスか判定します。
return value < 0.0;
}
};
{
final var stream = DoubleStream.of(-0.1, -0.2, -0.3);
final var ret = stream.anyMatch(predicate);
System.out.println(ret); // true
}
{
final var stream = DoubleStream.of(0.1, -0.2, 0.3);
final var ret = stream.anyMatch(predicate);
System.out.println(ret); // true
}
{
final var stream = DoubleStream.of(0.1, 0.2, 0.3);
final var ret = stream.anyMatch(predicate);
System.out.println(ret); // false
}
OptionalDouble average ()
final var stream = DoubleStream.of(1.0, 2.0, 3.0, 4.0, 5.0, 6.0);
final var opt = stream.average();
opt.ifPresent(value -> {
System.out.println(value); // 3.5
});
final var opt = DoubleStream.empty().average();
System.out.println(opt.isEmpty()); // true
Stream<Double> boxed ()
final Stream<Double> stream = DoubleStream.of(0.1, 0.2, 0.3).boxed();
final var ret = stream.toList();
System.out.println(ret); // [0.1, 0.2, 0.3]
static DoubleStream.Builder builder ()
final var builder = DoubleStream.builder();
builder.add(0.1).add(0.2).add(0.3);
final var stream = builder.build();
final var ret = stream.toArray();
System.out.println(Arrays.toString(ret)); // [0.1, 0.2, 0.3]
<R> R collect (Supplier<R> supplier, ObjDoubleConsumer<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 ObjDoubleConsumer<List<String>>() {
@Override
public void accept(List<String> list, double value) {
System.out.println("-- accumulator --");
System.out.println(" list = " + list);
System.out.println(" value = " + value);
list.add("%.2e".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 = DoubleStream.of(0.1, 0.2, 0.3);
final var ret = stream.collect(supplier, accumulator, combiner);
System.out.println("collect = " + ret);
// 結果
// ↓
//-- supplier --
//-- accumulator --
// list = []
// value = 0.1
//-- accumulator --
// list = [1.00e-01]
// value = 0.2
//-- accumulator --
// list = [1.00e-01, 2.00e-01]
// value = 0.3
//collect = [1.00e-01, 2.00e-01, 3.00e-01]
}
{
final var parallelStream = DoubleStream.of(0.1, 0.2, 0.3).parallel();
System.out.println(parallelStream.isParallel()); // true
final var ret = parallelStream.collect(supplier, accumulator, combiner);
System.out.println("collect = " + ret);
// 結果
// ↓
//-- supplier --
//-- accumulator --
// list = []
// value = 0.2
//-- supplier --
//-- accumulator --
// list = []
// value = 0.3
//-- supplier --
//-- accumulator --
// list = []
// value = 0.1
//-- combiner --
// list1 = [2.00e-01]
// list2 = [3.00e-01]
//-- combiner --
// list1 = [1.00e-01]
// list2 = [2.00e-01, 3.00e-01]
//collect = [1.00e-01, 2.00e-01, 3.00e-01]
}
static DoubleStream concat (DoubleStream a, DoubleStream b)
final var stream1 = DoubleStream.of(0.1, 0.2, 0.3);
final var stream2 = DoubleStream.of(7.0, 8.0, 9.0);
System.out.println(stream1.isParallel()); // false
System.out.println(stream2.isParallel()); // false
final var concatStream = DoubleStream.concat(stream1, stream2);
System.out.println(concatStream.isParallel()); // false
final var ret = concatStream.toArray();
System.out.println(Arrays.toString(ret)); // [0.1, 0.2, 0.3, 7.0, 8.0, 9.0]
final var stream1 = DoubleStream.of(0.1, 0.2, 0.3).parallel();
final var stream2 = DoubleStream.of(7.0, 8.0, 9.0).parallel();
System.out.println(stream1.isParallel()); // true
System.out.println(stream2.isParallel()); // true
final var concatStream = DoubleStream.concat(stream1, stream2);
System.out.println(concatStream.isParallel()); // true
final var ret = concatStream.toArray();
System.out.println(Arrays.toString(ret)); // [0.1, 0.2, 0.3, 7.0, 8.0, 9.0]
long count ()
final var stream = DoubleStream.of(0.1, 0.2, 0.3, 0.4);
System.out.println(stream.count()); // 4
final var stream = DoubleStream.empty();
System.out.println(stream.count()); // 0
DoubleStream distinct ()
final var stream = DoubleStream.of(1.0, 2.0, 2.0, 3.0, 3.0, 3.0);
final var ret = stream.distinct().toArray();
System.out.println(Arrays.toString(ret)); // [1.0, 2.0, 3.0]
default DoubleStream dropWhile (DoublePredicate predicate)
final var stream = DoubleStream.of(1.0, 2.0, Double.NaN, 4.0, 5.0);
final var predicate = new DoublePredicate() {
@Override
public boolean test(double value) {
return !Double.isNaN(value);
}
};
final var ret = stream.dropWhile(predicate).toArray();
System.out.println(Arrays.toString(ret)); // [NaN, 4.0, 5.0]
final var stream = DoubleStream.of(-1.0, -2.0, -3.0, Double.NaN, -5.0);
final var ret = stream.dropWhile(v -> !Double.isNaN(v)).toArray();
System.out.println(Arrays.toString(ret)); // [NaN, -5.0]
static DoubleStream empty ()
final var stream = DoubleStream.empty();
System.out.println(stream.count()); // 0
final var stream = DoubleStream.empty();
final var array = stream.toArray();
System.out.println(Arrays.toString(array)); // []
DoubleStream filter (DoublePredicate predicate)
final var stream = DoubleStream.of(0.1, -0.2, 0.3, -0.4);
final var predicate = new DoublePredicate() {
@Override
public boolean test(double value) {
// 値がマイナスか判定します。
return value < 0.0;
}
};
final var ret = stream.filter(predicate).toArray();
System.out.println(Arrays.toString(ret)); // [-0.2, -0.4]
final var stream = DoubleStream.of(0.1, -0.2, 0.3, -0.4);
final var ret = stream.filter(v -> v > 0.0).toArray();
System.out.println(Arrays.toString(ret)); // [0.1, 0.3]
OptionalDouble findAny ()
int count1 = 0;
int count2 = 0;
int count3 = 0;
for (int i = 0; i < 1000; i++) {
final var stream = DoubleStream.of(1.0, 2.0, 3.0).parallel();
System.out.println(stream.isParallel()); // true
// 1.0 or 2.0 or 3.0
final var opt = stream.findAny();
if (opt.isEmpty()) {
throw new IllegalStateException();
}
final var value = Math.round(opt.orElseThrow());
if (value == 1) {
count1++;
} else if (value == 2) {
count2++;
} else if (value == 3) {
count3++;
}
}
System.out.println("count1 : " + count1);
System.out.println("count2 : " + count2);
System.out.println("count3 : " + count3);
// 結果
// ↓
//count1 : 35
//count2 : 963
//count3 : 2
final var stream = DoubleStream.empty();
final var opt = stream.findAny();
System.out.println(opt.isEmpty()); // true
OptionalDouble findFirst ()
final var stream = DoubleStream.of(1.0, 2.0, 3.0);
final var opt = stream.findFirst();
opt.ifPresent(value -> {
System.out.println(value); // 1.0
});
// parallel
final var stream = DoubleStream.of(1.0, 2.0, 3.0).parallel();
System.out.println(stream.isParallel()); // true
final var opt = stream.findFirst();
opt.ifPresent(value -> {
System.out.println(value); // 1.0
});
int count1 = 0;
int count2 = 0;
int count3 = 0;
for (int i = 0; i < 1000; i++) {
// unordered and parallel
final var stream = DoubleStream.of(1.0, 2.0, 3.0).unordered().parallel();
// 1 or 2 or 3
final var opt = stream.findFirst();
if (opt.isEmpty()) {
throw new IllegalStateException();
}
final var value = Math.round(opt.orElseThrow());
if (value == 1) {
count1++;
} else if (value == 2) {
count2++;
} else if (value == 3) {
count3++;
}
}
System.out.println("count1 : " + count1);
System.out.println("count2 : " + count2);
System.out.println("count3 : " + count3);
// 結果
// ↓
//count1 : 79
//count2 : 919
//count3 : 2
final var stream = DoubleStream.empty();
final var opt = stream.findFirst();
System.out.println(opt.isEmpty()); // true
DoubleStream flatMap (DoubleFunction<? extends DoubleStream> mapper)
final var stream = DoubleStream.of(1.0, 2.0, 3.0);
final var mapper = new DoubleFunction<DoubleStream>() {
@Override
public DoubleStream apply(double value) {
return DoubleStream.of(value, value + 0.1, value + 0.2);
}
};
final var ret = stream.flatMap(mapper).toArray();
// [1.0, 1.1, 1.2, 2.0, 2.1, 2.2, 3.0, 3.1, 3.2]
System.out.println(Arrays.toString(ret));
void forEach (DoubleConsumer action)
関連 : forEachOrdered(DoubleConsumer action)
final var stream = DoubleStream.of(0.1, 0.2, 0.3);
final var action = new DoubleConsumer() {
@Override
public void accept(double value) {
System.out.println(value);
}
};
System.out.println("-- forEach --");
stream.forEach(action);
// 結果
// ↓
//-- forEach --
//0.1
//0.2
//0.3
// ラムダ式を使う例です。
final var stream = DoubleStream.of(-0.1, -0.2, -0.3);
System.out.println("-- forEach --");
stream.forEach(v -> {
System.out.println(v);
});
// 結果
// ↓
//-- forEach --
//-0.1
//-0.2
//-0.3
final var stream = DoubleStream.of(0.1, 0.2, 0.3, 0.4, 0.5).parallel();
System.out.println(stream.isParallel()); // true
System.out.println("-- forEach --");
stream.forEach(v -> {
System.out.println(v);
});
// 結果
// ↓
//-- forEach --
//0.3
//0.2
//0.1
//0.5
//0.4
void forEachOrdered (DoubleConsumer action)
final var stream = DoubleStream.of(0.1, 0.2, 0.3);
final var action = new DoubleConsumer() {
@Override
public void accept(double value) {
System.out.println(value);
}
};
System.out.println("-- forEach --");
stream.forEachOrdered(action);
// 結果
// ↓
//-- forEach --
//0.1
//0.2
//0.3
final var stream = DoubleStream.of(0.1, 0.2, 0.3, 0.4, 0.5).parallel();
System.out.println(stream.isParallel()); // true
System.out.println("-- forEach --");
stream.forEachOrdered(v -> {
System.out.println(v);
});
// 結果
// ↓
//-- forEach --
//0.1
//0.2
//0.3
//0.4
//0.5
static DoubleStream generate (DoubleSupplier s)
final var supplier = new DoubleSupplier() {
double count;
@Override
public double getAsDouble() {
count += 10.0;
return count;
}
};
final var stream = DoubleStream.generate(supplier);
final var ret = stream.limit(5).toArray();
System.out.println(Arrays.toString(ret)); // [10.0, 20.0, 30.0, 40.0, 50.0]
static DoubleStream iterate (double seed, DoublePredicate hasNext, DoubleUnaryOperator next)
final var hasNext = new DoublePredicate() {
@Override
public boolean test(double value) {
return value <= 100.0;
}
};
final var next = new DoubleUnaryOperator() {
@Override
public double applyAsDouble(double operand) {
return operand * 2.0;
}
};
final var stream = DoubleStream.iterate(1.0, hasNext, next);
final var ret = stream.toArray();
// [1.0, 2.0, 4.0, 8.0, 16.0, 32.0, 64.0]
System.out.println(Arrays.toString(ret));
static DoubleStream iterate (double seed, DoubleUnaryOperator f)
final var next = new DoubleUnaryOperator() {
@Override
public double applyAsDouble(double operand) {
return operand * 2.0;
}
};
final var stream = DoubleStream.iterate(1.0, next);
final var ret = stream.limit(10).toArray();
// [1.0, 2.0, 4.0, 8.0, 16.0, 32.0, 64.0, 128.0, 256.0, 512.0]
System.out.println(Arrays.toString(ret));
DoubleStream limit (long maxSize)
final var stream = DoubleStream.of(0.1, 0.2, 0.3, 0.4, 0.5);
final var ret = stream.limit(3).toArray();
System.out.println(Arrays.toString(ret)); // [0.1, 0.2, 0.3]
DoubleStream map (DoubleUnaryOperator mapper)
final var stream = DoubleStream.of(0.1, 0.2, 0.3);
final var mapper = new DoubleUnaryOperator() {
@Override
public double applyAsDouble(double operand) {
return operand * 2.0;
}
};
final var ret = stream.map(mapper).toArray();
System.out.println(Arrays.toString(ret)); // [0.2, 0.4, 0.6]
final var stream = DoubleStream.of(1.0, 2.0, 3.0);
final var ret = stream.map(v -> v * 1.5).toArray();
System.out.println(Arrays.toString(ret)); // [1.5, 3.0, 4.5]
default DoubleStream mapMulti (DoubleStream.DoubleMapMultiConsumer mapper)
final var stream = DoubleStream.of(1.0, 2.0, 3.0);
final var mapper = new DoubleStream.DoubleMapMultiConsumer() {
@Override
public void accept(double value, DoubleConsumer dc) {
dc.accept(value);
dc.accept(value + 0.1);
dc.accept(value + 0.2);
}
};
final var ret = stream.mapMulti(mapper).toArray();
// [1.0, 1.1, 1.2, 2.0, 2.1, 2.2, 3.0, 3.1, 3.2]
System.out.println(Arrays.toString(ret));
IntStream mapToInt (DoubleToIntFunction mapper)
final var stream = DoubleStream.of(1.1, 2.2, 3.3);
final var mapper = new DoubleToIntFunction() {
@Override
public int applyAsInt(double value) {
final var rounded = Math.round(value);
return Math.toIntExact(rounded);
}
};
final var ret = stream.mapToInt(mapper).toArray();
System.out.println(Arrays.toString(ret)); // [1, 2, 3]
LongStream mapToLong (DoubleToLongFunction mapper)
final var stream = DoubleStream.of(1.0e10, 2.0e10);
final var mapper = new DoubleToLongFunction() {
@Override
public long applyAsLong(double value) {
return Math.round(value);
}
};
final var ret = stream.mapToLong(mapper).toArray();
System.out.println(Arrays.toString(ret)); // [10000000000, 20000000000]
<U> Stream<U> mapToObj (DoubleFunction<? extends U> mapper)
final var stream = DoubleStream.of(0.1, 0.2, 0.3);
final var mapper = new DoubleFunction<String>() {
@Override
public String apply(double value) {
return "num=" + value;
}
};
final var ret = stream.mapToObj(mapper).toList();
System.out.println(ret); // [num=0.1, num=0.2, num=0.3]
OptionalDouble max ()
final var stream = DoubleStream.of(7.0, 99.0, 0.1, 5.0);
final var opt = stream.max();
opt.ifPresent(value -> {
System.out.println(value); // 99.0
});
final var stream = DoubleStream.empty();
final var opt = stream.max();
System.out.println(opt.isEmpty()); // true
OptionalDouble min ()
final var stream = DoubleStream.of(7.0, 99.0, 0.1, 5.0);
final var opt = stream.min();
opt.ifPresent(value -> {
System.out.println(value); // 0.1
});
final var stream = DoubleStream.empty();
final var opt = stream.min();
System.out.println(opt.isEmpty()); // true
boolean noneMatch (DoublePredicate predicate)
final var predicate = new DoublePredicate() {
@Override
public boolean test(double value) {
// 値がマイナスか判定します。
return value < 0.0;
}
};
{
final var stream = DoubleStream.of(-0.1, -0.2, -0.3);
final var ret = stream.noneMatch(predicate);
System.out.println(ret); // false
}
{
final var stream = DoubleStream.of(0.1, -0.2, 0.3);
final var ret = stream.noneMatch(predicate);
System.out.println(ret); // false
}
{
final var stream = DoubleStream.of(0.1, 0.2, 0.3);
final var ret = stream.noneMatch(predicate);
System.out.println(ret); // true
}
static DoubleStream of (double t)
final var stream = DoubleStream.of(0.0);
final var ret = stream.toArray();
System.out.println(Arrays.toString(ret)); // [0.0]
final var stream = DoubleStream.of(1.23);
final var ret = stream.toArray();
System.out.println(Arrays.toString(ret)); // [1.23]
static DoubleStream of (double... values)
final var stream = DoubleStream.of(0.1, 0.2, 0.3);
final var ret = stream.toArray();
System.out.println(Arrays.toString(ret)); // [0.1, 0.2, 0.3]
DoubleStream peek (DoubleConsumer action)
final var stream = DoubleStream.of(0.1, 0.2, 0.3);
final var action = new DoubleConsumer() {
@Override
public void accept(double value) {
System.out.println("peek : value = " + value);
}
};
final var array = stream.peek(action).toArray();
System.out.println("array = " + Arrays.toString(array));
// 結果
// ↓
//peek : value = 0.1
//peek : value = 0.2
//peek : value = 0.3
//array = [0.1, 0.2, 0.3]
double reduce (double identity, DoubleBinaryOperator op)
final var stream = DoubleStream.of(1.0, 2.0, 3.0, 4.0);
final var identity = 0.0;
final var op = new DoubleBinaryOperator() {
@Override
public double applyAsDouble(double left, double right) {
if (identity == left) {
return right;
}
return left + right;
}
};
// 1.0 + 2.0 + 3.0 + 4.0 = 10.0
final var ret = stream.reduce(identity, op);
System.out.println(ret); // 10.0
final var stream = DoubleStream.empty();
final var identity = 0.0;
final var ret = stream.reduce(identity, (left, right) -> {
if (identity == left) {
return right;
}
return left + right;
});
System.out.println(ret); // 0.0
OptionalDouble reduce (DoubleBinaryOperator op)
final var stream = DoubleStream.of(1.0, 2.0, 3.0, 4.0);
final var op = new DoubleBinaryOperator() {
@Override
public double applyAsDouble(double left, double right) {
return left + right;
}
};
// 1.0 + 2.0 + 3.0 + 4.0 = 10.0
final var opt = stream.reduce(op);
opt.ifPresent(value -> {
System.out.println(value); // 10.0
});
final var stream = DoubleStream.empty();
final var opt = stream.reduce((left, right) -> left + right);
System.out.println(opt.isEmpty()); // true
DoubleStream skip (long n)
final var stream = DoubleStream.of(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7);
final var ret = stream.skip(4).toArray();
System.out.println(Arrays.toString(ret)); // [0.5, 0.6, 0.7]
DoubleStream sorted ()
final var stream = DoubleStream.of(0.2, 0.5, 0.4, 0.1, 0.3);
final var ret = stream.sorted().toArray();
System.out.println(Arrays.toString(ret)); // [0.1, 0.2, 0.3, 0.4, 0.5]
final var stream = DoubleStream.of(0.2, 0.5, 0.4, 0.1, 0.3).parallel();
System.out.println(stream.isParallel()); // true
final var ret = stream.sorted().toArray();
System.out.println(Arrays.toString(ret)); // [0.1, 0.2, 0.3, 0.4, 0.5]
double sum ()
final var stream = DoubleStream.of(1.0, 2.0, 3.0, 4.0, 5.0);
System.out.println(stream.sum()); // 15.0
final var stream = DoubleStream.of(-0.1, -0.2, -0.3);
System.out.println(stream.sum()); // -0.6
DoubleSummaryStatistics summaryStatistics ()
final var stream = DoubleStream.of(0.1, 0.2, 0.3, 0.4, 0.5);
final var ret = stream.summaryStatistics();
// DoubleSummaryStatistics{count=5, sum=1.500000, min=0.100000, average=0.300000, max=0.500000}
System.out.println(ret);
default DoubleStream takeWhile (DoublePredicate predicate)
final var stream = DoubleStream.of(1.0, 2.0, 3.0, Double.NaN, 5.0);
final var predicate = new DoublePredicate() {
@Override
public boolean test(double value) {
return !Double.isNaN(value);
}
};
final var ret = stream.takeWhile(predicate).toArray();
System.out.println(Arrays.toString(ret)); // [1.0, 2.0, 3.0]
final var stream = DoubleStream.of(-1.0, -2.0, Double.NaN, -4.0, -5.0);
final var ret = stream.takeWhile(v -> !Double.isNaN(v)).toArray();
System.out.println(Arrays.toString(ret)); // [-1.0, -2.0]
double[] toArray ()
final var stream = DoubleStream.of(0.1, 0.2, 0.3);
final double[] ret = stream.toArray();
System.out.println(Arrays.toString(ret)); // [0.1, 0.2, 0.3]
BaseStreamで宣言されたメソッド
close, isParallel, iterator, onClose, parallel, sequential, spliterator, unordered
「Java API 使用例 : BaseStream」をご参照ください。