Comparison to Java 7
Learn about the difference between Java 7 and Java 8 code.
Java 7 vs. Java 8
To better illustrate the benefit of Streams in Java 8, here are some examples of code from Java 7 compared to their new versions.
Find a maximum
Press + to interact
import java.util.Arrays;import java.util.Collections;import java.util.Comparator;import java.util.List;import java.util.stream.IntStream;import java.util.stream.Stream;public class Compare {public double max(List<Double> list) {assert list.size() > 0;// Java 7double max = 0;for (Double d : list) {if (d > max) {max = d;}}return max;}public static void main(String[] args) {Compare c = new Compare();System.out.println(c.max(Arrays.asList(0.1, 0.2, 0.21)));}}
The above code finds the maximum value in a list of numbers. In Java 7, the implementation uses a for
loop to iterate through the list and initialize max
with the largest value. ...
Access this course and 1400+ top-rated courses and projects.