Naveen Karthikeyan
Naveen Karthikeyan

Reputation: 77

What is Stream API calls in Java

I used for each loop to traverse through an ArrayList consisting an object type. I used intellij. And i get a message on the for each loop which reads

This inspection reports for each loops which can be replaced with stream api call

What does that mean? Please help. Thanks.

Upvotes: 1

Views: 2279

Answers (2)

Prashanth
Prashanth

Reputation: 954

Stream API is a new feature provided by Java 8 , which allows you to perform operations ( filter, map, sort, reduce etc ) on Collections such as list, queue etc in sequential or parallel ways and in simpler ways.

Have a look at below article which explains its use with a short example :

Java 8 - Stream API - short examples

Upvotes: 0

urag
urag

Reputation: 1258

It is called internal iteration one of many cool features of Java 8 here is an example

package com.just.stuff;

import java.util.Arrays;
import java.util.List;

public class TestClass{
    public static void main(String[] args) {
        List<String> theList = Arrays.asList("A","B","C");

        // The old way
        for (String str:theList ) {
            System.out.println(str);
        }


        // The new way 
        theList.stream().forEach(str->{
            System.out.println(str);
        });
    }
}

Here is a nice link

Upvotes: 4

Related Questions