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()));
}
}
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()));
}
}
ReplyDeleteThis 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
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.
ReplyDeleteJava Training Institute in Chennai
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.
ReplyDeleteDigital Marketing Company in Chennai
Wowwww... really great information. Having interesting topic. The pictorial representation helps me to easy understanding of this. Have a great idea. Thank you.
ReplyDeleteWeb Designing Training in Chennai
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.
ReplyDeleteWeb Designing Training in Chennai
ReplyDeletekeep 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
I like this blog so much because share very useful information its improve my knowledge & Career .Thank you so much.| Java Training in Chennai |
ReplyDeleteBig Data Training in Chennai
SAS Training in Chennai
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.
ReplyDeleteThis 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.
ReplyDeleteGreat 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.
ReplyDeleteSAP HR Training in Chennai
SAP ABAP Training in Chennai
SAP FICO Training in Chennai