Tuesday, 8 May 2018



Java Lambda Expression Example: Filter Collection Data



package govind;

public class User {
           
            int uid;
            String uname;
              float usalary;
            String email;
            public int getUid() {
                        return uid;
            }
            public void setUid(int uid) {
                        this.uid = uid;
            }
            public String getUname() {
                        return uname;
            }
            public void setUname(String uname) {
                        this.uname = uname;
            }
            public float getUsalary() {
                        return usalary;
            }
            public void setUsalary(float usalary) {
                        this.usalary = usalary;
            }
            public String getEmail() {
                        return email;
            }
            public void setEmail(String email) {
                        this.email = email;
            }
            }


package govind;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

public class UserMain {
            public static void main(String[] args) {
                        List<User> l=new ArrayList<>();
                        User u1=new User();
                        u1.setUid(101);
                        u1.setUname("govind");
                        u1.setUsalary(21000.00f);
                        u1.setEmail("govindkhan@gmail.com");
                        l.add(u1);
                       
                        User u2=new User();
                        u2.setUid(105);
                        u2.setUname("ajit");
                        u2.setUsalary(23000.00f);
                        u2.setEmail("ajitraj@gmail.com");
                        l.add(u2);
                       

                        User u3=new User();
                        u3.setUid(104);
                        u3.setUname("mani");
                        u3.setUsalary(17000.00f);
                        u3.setEmail("manibhushan@gmail.com");
                        l.add(u3);
                       
                       

                        User u4=new User();
                        u4.setUid(109);
                        u4.setUname("sanjeet");
                        u4.setUsalary(18000.00f);
                        u4.setEmail("sanjeet@gmail.com");
                        l.add(u4);
                       

                        User u5=new User();
                        u5.setUid(108);
                        u5.setUname("dinesh");
                        u5.setUsalary(16000.00f);
                        u5.setEmail("dinesh@gmail.com");
                        l.add(u5);
                       
                       



                        //using lambda to filter data
                        Stream<User> fdata = l.stream().filter(u->u.usalary>20000.00f);
                        //using lambda to iterate through collection
                       
                        fdata.forEach(
                                               
u->System.out.println(u.getUname()+" "+u.getUid()+" "+u.getEmail()+" "+u.getUsalary())
                                               
                                                );
                       
}

}
o/p:-
govind 101 govindkhan@gmail.com 21000.0
ajit 105 ajitraj@gmail.com 23000.0








No comments:

Post a Comment