Stream API

In java stream api is used to fetch something and then filter the result according to the requiremnt.

It is a generic approach which is used on Generic class.
The Stream api is used to fetch the stream, the filter api to filter the required information  ,the map api is used to map the function which are filtered and the  forEach  api to display the filtered mapped output.

given an example :-


package com.test;

import java.util.ArrayList;
import java.util.List;

class productPrice {
    int price;

    public productPrice(int price) {
        super();
        this.price = price;
    }

}

public class TestStream {

    public static void main(String[] args) {
        List<productPrice> abc = new ArrayList<productPrice>();
        abc.add(new productPrice(4));
        abc.add(new productPrice(9));
        abc.add(new productPrice(8));

        abc.stream().filter(k -> k.price > 4).map(r -> r.price)

                .forEach(System.out::println);
        ;

    }

}

************************** OUTPUT ****************************************

9
8

No comments:

Post a Comment