I am new to Java 8 threads, and I am wondering if there is a way to make the call forEach/mapby the method returning byteand accept the intas parameter .
Example:
public class Test {
private byte[] byteArray;
public byte getByte(int index) {
return this.byteArray[index];
}
public byte[] getBytes(int... indexes) {
return Stream.of(indexes)
.map(this::getByte)
.collect(byte[]::new);
}
}
As you can guess, the method getBytesdoes not work. "int[] cannot be converted to int"There is probably no foreach somewhere, but he personally cannot understand it.
However, this works, an old-fashioned approach that I would like to rewrite as Stream.
byte[] byteArray = new byte[indexes.length];
for ( int i = 0; i < byteArray.length; i++ ) {
byteArray[i] = this.getByte( indexes[i] );
}
return byteArray;
glf4k source
share