Wednesday, 9 May 2018


Java Stream Example: Filtering and Iterating Collection




package streamconcept;

public class State {
                int stateCode;
                String stateName;
                String stateCmname;
                public State(int stateCode, String stateName, String stateCmname) {
                                super();
                                this.stateCode = stateCode;
                                this.stateName = stateName;
                                this.stateCmname = stateCmname;
                }
               
               

}

package streamconcept;

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

public class StateMain {
           
            public static void main(String[] args) {
            List<State> l=new ArrayList<>();
            State s1=new State(101, "bihar", "Nitish kumar");
            State s2=new State(102, "mp", "Shivraj singh");
            State s3=new State(103, "up", "Yogi adityanath");
            State s4=new State(104, "delhi", "Kejriwal");
            State s5=new State(105, "rajasthan", "Vashundhara raje");
            l.add(s1);
            l.add(s2);
            l.add(s3);
            l.add(s4);
            l.add(s5);
            l.stream()
            .filter(sn->sn.stateName.equalsIgnoreCase("bihar"))
            .forEach(sn->System.out.println(sn.stateCmname));
           
           
            }

}

o/p:-------------
Nitish kumar

No comments:

Post a Comment