✨ New update: Automation 2.0 is live — smarter workflows, faster results.

Java 8 Streams and Lambda Expressions

Introduction Java 8 is a game-changer for developers, with the introduction of new features like Lambda expressions and Stream API. The Stream API provides a powerful and efficient way to process large data sets and Lambda expressions provide a concise way to write functional code. In this article, we’ll take a closer look at these …

Introduction

Java 8 is a game-changer for developers, with the introduction of new features like Lambda expressions and Stream API. The Stream API provides a powerful and efficient way to process large data sets and Lambda expressions provide a concise way to write functional code. In this article, we’ll take a closer look at these two features and see how they can be used together to write concise, readable and efficient code.

Stream API

The Stream API is a powerful tool for processing large data sets in an efficient manner. It provides a way to perform operations such as filtering, mapping, and reducing on collections of data. With the Stream API, it is possible to process large data sets in parallel, making it an ideal solution for dealing with big data.

One of the main advantages of the Stream API is that it is lazy. This means that the operations performed on a stream are not executed until a terminal operation is performed. Terminal operations are operations that produce a result, such as a count, a sum, or a collection of results.

Code Example:

[/fusion_text][fusion_syntax_highlighter language=”” copy_to_clipboard_text=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility”]TGlzdDxJbnRlZ2VyPiBudW1iZXJzID0gQXJyYXlzLmFzTGlzdCgxLCAyLCAzLCA0LCA1LCA2LCA3LCA4LCA5LCAxMCk7CgppbnQgc3VtID0gbnVtYmVycy5zdHJlYW0oKQogIC5maWx0ZXIobiAtPiBuICUgMiA9PSAwKQogIC5tYXBUb0ludChJbnRlZ2VyOjppbnRWYWx1ZSkKICAuc3VtKCk7CgpTeXN0ZW0ub3V0LnByaW50bG4oIlN1bSBvZiBldmVuIG51bWJlcnM6ICIgKyBzdW0pOw==[/fusion_syntax_highlighter][fusion_text]

In the above code example, we have a list of numbers and we want to calculate the sum of even numbers. We first create a stream from the list and then perform the following operations on the stream:

  • filter: This operation filters the elements in the stream based on the condition specified in the lambda expression. In this case, we only keep the elements that are even.
  • mapToInt: This operation maps the elements in the stream to their integer values.
  • sum: This is a terminal operation that returns the sum of the elements in the stream.

Lambda Expressions

Lambda expressions are a new feature introduced in Java 8 that allows developers to write functional code in a concise manner. A lambda expression is a small piece of code that can be passed around as a value.

Code Example:

[/fusion_text][fusion_syntax_highlighter language=”” copy_to_clipboard_text=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility”]TGlzdDxJbnRlZ2VyPiBudW1iZXJzID0gQXJyYXlzLmFzTGlzdCgxLCAyLCAzLCA0LCA1LCA2LCA3LCA4LCA5LCAxMCk7Cm51bWJlcnMuZm9yRWFjaChuIC0+IFN5c3RlbS5vdXQucHJpbnRsbihuKSk7[/fusion_syntax_highlighter][fusion_text]

In the above code example, we have a list of numbers and we want to print each number. We can use the forEach method from the List interface and pass a lambda expression to it. The lambda expression takes an integer as an argument and prints it to the console.

Lambda expressions can also be used in conjunction with the Stream API to write concise and readable code.

Code Example:

[/fusion_text][fusion_syntax_highlighter language=”” copy_to_clipboard_text=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility”]TGlzdDxQZXJzb24+IHBlb3BsZSA9IEFycmF5cy5hc0xpc3QoCiAgbmV3IFBlcnNvbigiSm9obiIsICJEb2UiLCAzMCksCiAgbmV3IFBlcnNvbigiSmFuZSIsICJEb2UiLCAyNSksCiAgbmV3IFBlcnNvbigiSmltIiwgIlNtaXRoIiwgMzUpCik7CgpwZW9wbGUuc3RyZWFtKCkKICAuZmlsdGVyKHAgLT4gcC5nZXRBZ2UoKSA+PSAzMCkKICAuZm9yRWFjaChwIC0+IFN5c3RlbS5vdXQucHJpbnRsbihwKSk7Cg==[/fusion_syntax_highlighter][fusion_text]

In the above code example, we have a list of people and we want to print the details of people who are 30 years.

That’s it for this blog. Thank you for reading.

[/fusion_text][/fusion_builder_column][/fusion_builder_row][/fusion_builder_container]

ali.akhwaja@gmail.com

ali.akhwaja@gmail.com

Related Posts

Kafka is widely used message broker especially in distributed systems, many ask this question that why Kafka is preferred over other available message brokers. There is no clear answer to this question instead it depends on your own requirements. Here we will discuss fundamentals of Kafka which you should know to get started. What is …

Software project management is an art and science of planning and leading software projects. It is a sub-discipline of project management in which software projects are planned, implemented, monitored and controlled. A software project manager is the most important person inside a team who takes the overall responsibilities to manage the software projects and play …

Leave a Reply

Your email address will not be published. Required fields are marked *