Pages

Showing posts with label Lambda. Show all posts
Showing posts with label Lambda. Show all posts

Monday, December 6, 2021

Java 8 Comparator Lambda Examples

1. Overview


In this tutorial, We'll learn how to use the Comparator interface with lambda expression in Java 8 onwards.

Lambda expression is to replace the anonymous implementations and to reduce the boilerplate code.

This makes the code cleaner and focus more on the business logic rather than semantics.


Java community provided a few sets of rules and guidelines in creating the lambda statements.

Lambda syntax and invalid one are discussed in detail in the previous discussions "java 8 lambda expressions".

First, we will see the examples without lambda and then the next section will see the equivalent lambdas.

For sorting to work with lambda comparator, we have to use the Collections.sort() method.

Java Comparator Lambda Examples

Monday, July 1, 2019

Java 8: Accessing Variables from Lambda Expressions

1. Overview

In this tutorial, We'll learn about how to access variables from Lambda Expressions in Java 8.

Before that, We suggest you read the tutorial on Java 8 Lambda Expressions discussed in the previous tutorial.

If you already know about Lambda's, just ignore and continue reading this article.

Can we declare a variable inside Lambda Expression?

Java 8: Accessing Variables from Lambda Expressions


The answer is yes. We can declare variables as many as needed.

What do you think if lambda has a reference to another variable?

Here, We have two scenarios to know.

First, What will happen for local variables in Lambda.

Second, Non-local variables to Lambda.