Java : DoubleConsumer with Examples
DoubleConsumer (Java SE 21 & JDK 21) with Examples.
You will find code examples on most DoubleConsumer methods.
Summary
final var stream = DoubleStream.of(0.25, 0.5, 1.0, 2.0);
final var action = new DoubleConsumer() {
@Override
public void accept(double value) {
final var hex = Double.toHexString(value);
System.out.println(hex);
}
};
System.out.println("-- forEach --");
stream.forEach(action);
// Result
// ↓
//-- forEach --
//0x1.0p-2
//0x1.0p-1
//0x1.0p0
//0x1.0p1
// An example with a lambda expression.
final var stream = DoubleStream.of(0.25, 0.5, 1.0, 2.0);
System.out.println("-- forEach --");
stream.forEach(value -> {
final var hex = Double.toHexString(value);
System.out.println(hex);
});
// Result
// ↓
//-- forEach --
//0x1.0p-2
//0x1.0p-1
//0x1.0p0
//0x1.0p1
Methods
void accept (double value)
final var stream = DoubleStream.of(0.25, 0.5, 1.0, 2.0);
final var action = new DoubleConsumer() {
@Override
public void accept(double value) {
final var hex = Double.toHexString(value);
System.out.println(hex);
}
};
System.out.println("-- forEach --");
stream.forEach(action);
// Result
// ↓
//-- forEach --
//0x1.0p-2
//0x1.0p-1
//0x1.0p0
//0x1.0p1
// An example with a lambda expression.
final var stream = DoubleStream.of(0.25, 0.5, 1.0, 2.0);
System.out.println("-- forEach --");
stream.forEach(value -> {
final var hex = Double.toHexString(value);
System.out.println(hex);
});
// Result
// ↓
//-- forEach --
//0x1.0p-2
//0x1.0p-1
//0x1.0p0
//0x1.0p1
default DoubleConsumer andThen (DoubleConsumer after)
final var before = new DoubleConsumer() {
@Override
public void accept(double value) {
System.out.println("before : " + value);
}
};
final var after = new DoubleConsumer() {
@Override
public void accept(double value) {
System.out.println("after : " + value);
}
};
final var consumer = before.andThen(after);
consumer.accept(1.23);
// Result
// ↓
//before : 1.23
//after : 1.23