adam bien's blog

Java 8: From Ordinary For-Loop To An IntStream 📎

Legacy:


for (int i = 0; i < 10; i++) {
	System.out.println(i);
}

Fancy:


IntStream.range(0, 10).forEach(
	nbr -> System.out.println(nbr)
);

Why? ...because the execution of the following snippet takes 1 second and not 10 seconds:


IntStream.range(0, 10).parallel().forEach(
	nbr -> {
	try {
		Thread.sleep(1000);
		} catch (InterruptedException ex) {}
        	System.out.println(nbr);
        }
);