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

Popular posts from this blog

a simple example for jdbc PreparedStatement

a simple example for PreparedStatement package basics.in.java.blogspot.in; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Main { private static final String USERNAME="root"; private static final String PASSWORD=""; private static final String CONN_STRING="jdbc:mysql://localhost/basicsinjavablogspot"; public static void main(String[] args) throws ClassNotFoundException, SQLException { Class.forName("com.mysql.jdbc.Driver"); Connection conn=null; Statement stmt=null; ResultSet rs=null; try { conn= DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD); System.out.println("database connection successful"); //stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); String sql="select * fr...

Server-Side Pagination with React-Table and Spring Boot JPA with H2 Database

Pagination is a common technique used to split large amounts of data into smaller, more manageable chunks. With server-side pagination, data is retrieved from the server in smaller batches, reducing the amount of data transferred over the network and improving application performance. React-Table provides a wide range of built-in features such as sorting, filtering, pagination, row selection, and column resizing. These features can be easily configured and customized to fit specific requirements. For example, you can customize the sorting behavior to handle multiple sorting criteria, or you can add custom filters to the table to handle complex data filtering scenarios. Additionally, React-Table provides a flexible API that allows developers to extend its functionality with custom hooks, plugins, and components. This means that you can easily add custom functionality to the table, such as exporting data to CSV or integrating with external data sources. In terms of styl...