Skip to main content

processing java8 collections with streams and lambda expression

As you all know java 8 introduced many new features ; and one of them is lambda expressions , those are very helpful to reduce some boilerplate code . I have tried some of them today those are very helpful and tricky code ; iterating over collection can be done in one or two lines of code . It can be used in multithreaded environment as well.


package java8Works;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Stream;

class Person {
Person(String name, int age) {
this.name = name;
this.age = age;

}

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

private int age;
}

public class Java8 {

public static void main(String[] args) {
Person[] people = { new Person(null, 26), new Person("manu", 26) };
Stream<Person> stream = Stream.of(people);
stream.forEach(p -> {
System.out.println(p.getName());
});

List<Person> peopleList=new ArrayList<Person>();
peopleList.add(new Person("manu", 27));
peopleList.add(new Person("sudeep", 26));
peopleList.add(new Person(null,2));
peopleList.add(new Person("Arun", 29));
peopleList.forEach(p ->{
System.out.println(p.getName());
});

Predicate<Person> predicate=new Predicate<Person>() {

@Override
public boolean test(Person t) {

return t!=null ? true:false;
}
};

Predicate<Person> newWaypredicate=(p)-> p!=null?true:false;

peopleList.forEach(p->{
if(predicate.test(p)){
System.out.println(p.toString());

}
});

peopleList.stream()
.filter(newWaypredicate)
.forEach(p->System.out.println(p.getAge()));
 


}

}

Comments


  1. This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.

    Online Reputation Management

    ReplyDelete
  2. this is really too useful and have more ideas from yours. keep sharing many techniques. eagerly waiting for your new blog and useful information. keep doing more.
    Java Training Institute in Chennai

    ReplyDelete
  3. Hi, Your post is quite great to view and easy way to grab the extra knowledge. Thank you for your share with us. I like to visit your site again for my future reference.

    Digital Marketing Company in Chennai

    ReplyDelete
  4. Wowwww... really great information. Having interesting topic. The pictorial representation helps me to easy understanding of this. Have a great idea. Thank you.
    Web Designing Training in Chennai

    ReplyDelete
  5. Thank you for sharing like this information. This is the most easy way of learning. This helps me to get some idea regarding this and helps me to bring a creative thought.
    Web Designing Training in Chennai

    ReplyDelete

  6. keep sharing your information regularly for my future reference. This content creates a new hope and inspiration with in me


    Car Spa at Doorstep in Mumbai

    ReplyDelete
  7. I like this blog so much because share very useful information its improve my knowledge & Career .Thank you so much.| Java Training in Chennai |
    Big Data Training in Chennai
    SAS Training in Chennai

    ReplyDelete
  8. Computing languages might be mushrooming in number but JAVA is considered the mother of all. Simple reasons being, its more than a decade contributions in the IT sector. Also the omnivorous presence of Java irrespective of the kind of device – computer or mobile, makes Java a must learn language especially for budding coders.

    ReplyDelete
  9. This is Much Informative blog on Java. Thanks for posting such an informative articles. After completing my basic Java Tutorial, I have continued on this site.

    ReplyDelete
  10. Great post! I am actually getting ready to across this information, It's very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
    SAP HR Training in Chennai
    SAP ABAP Training in Chennai
    SAP FICO Training in Chennai

    ReplyDelete

Post a Comment