Change the Better World

Stream in Java 본문

Programming/Java

Stream in Java

white_cheetah 2022. 9. 27. 07:08

Stream is an iterator that allows you to refer to the storage elements of a collection one by one and process them in a lambda expression, which has been added since Java 8. It acts similar to an Iterator, but the difference is that the code can be made more concise by providing the element handling code in a lambda expression, and that parallelism is easy because it uses an internal iterator.

Iterator vs Stream

        // Iterator
        List<String> list = Arrays.asList("Kim", "Sin", "Park");
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            String name = iterator.next();
            System.out.println(name);
        }

        // Stream
        Stream<String> stream = list.stream();
        stream.forEach(name -> System.out.println(name));

'Programming > Java' 카테고리의 다른 글

what is the var keyword in Java 10?  (0) 2022.09.26
Difference between String, StringBuilder, and StringBuffer.  (0) 2021.11.21
JDK, JRE, and JVM  (0) 2021.11.21
Comments