我是Java 8的新手.我仍然不深入了解API,但我已经做了一个小的非正式基准测试来比较新Streams API与优秀旧Collections的性能.
测试包括过滤一个列表Integer,并为每个偶数计算平方根并将其存储在结果List中Double.
这是代码:
public static void main(String[] args) {
//Calculating square root of even numbers from 1 to N
int min = 1;
int max = 1000000;
List<Integer> sourceList = new ArrayList<>();
for (int i = min; i < max; i++) {
sourceList.add(i);
}
List<Double> result = new LinkedList<>();
//Collections approach
long t0 = System.nanoTime();
long elapsed = 0;
for (Integer i : sourceList) {
if(i % 2 == 0){
result.add(Math.sqrt(i));
} …Run Code Online (Sandbox Code Playgroud) Loop.times(5, () -> {
System.out.println("looping");
});
Run Code Online (Sandbox Code Playgroud)
哪些有效编译到?
for(int i = 0; i < 5; i++)
System.out.println("looping");
Run Code Online (Sandbox Code Playgroud)
或类似的东西
new CallableInterfaceImpl(){
public void call(){
for(int i = 0; i < 5; i++)
System.out.println("looping");
}
}.call();
Run Code Online (Sandbox Code Playgroud)
它会替换(内联类),还是实际创建一个匿名类?