If you have an Array and you want to use the Java8 forEach() method, which approach is better or more efficient:
Arrays.asList(new String[]{\"hallo\",\"hi\"}
Arrays.asList() method................: 22 ms
Arrays.stream() method................: 26 ms
Stream.of() method....................: 26 ms
Arrays.asList() (premade array) method: 8 ms
Arrays.stream() (premade array) method: 30 ms
Stream.of() (premade array) method....: 17 ms
When you change doSomething to actually do nothing as follows:
public static void doSomething(String s){
}
Then you're measuring the actual speed of these operations instead of the operation String = String + String; This is what doSomething was doing, and of course it is about the same speed consistently. However, the actual speed isn't the same and asList with a premade array is much faster.
The real result here has already been noted by others that you should beware of the stream as it is typically 4 times slower than the plain old java (non-lambda) approach.