Java8: Lambdas & Streams
When streams combined with lambdas we can do a lot with very few lines of codes. Let’s try to print below class using streams.
public class Person{
String name;
int age;
Person(String name, int age){
this.age=age;
this.name=name;
}
}
First we will convert the list to a stream and do a filter to find the names start with s. Then filtered results to be printed.
List<Person> list= Arrays.asList(
new Person("nuwan",20),
new Person("sen",30)
);
list.stream()
.filter(person -> person.name.startsWith("s"))
.forEach(person -> System.out.println(person.name));
Read my other blogs: