lambda expression in java interview questions

Aidan 295 Published: 06/01/2024

lambda expression in java interview questions

I'd be happy to help with that!

Lambda expressions are a powerful feature in Java introduced in version 8 (Java SE 8). They allow you to write concise and expressive code, making your programming life easier. Here are some common lambda expression-related interview questions:

What is the main purpose of using lambda expressions in Java?

Answer: Lambda expressions enable functional programming in Java, allowing you to write small, anonymous functions that can be used as arguments to methods or returned from methods.

Can you give an example of a simple lambda expression?

Example:

Runnable r = () -> System.out.println("Hello");

r.run(); // prints "Hello"

This lambda expression defines an anonymous Runnable interface, which is executed when the run() method is called.

How do you use a lambda expression as a method reference?

Example:

 Comparator c = String::compareTo;

System.out.println(c.compare("hello", "world")); // prints -1

In this example, we create a lambda expression that refers to the compareTo() method of the String class. This can be useful when you need to pass an existing method as a callback.

Can you explain the difference between a lambda expression and a functional interface?

Answer: A functional interface is an interface with a single abstract method (SAM). Lambda expressions are instances of functional interfaces that can be used to implement them.

How do you use a lambda expression to filter a collection?

Example:

List list = Arrays.asList("hello", "world", "lambda");

list.stream()

.filter(s -> s.length() > 4)

.collect(Collectors.toList());

// returns ["lambda"]

In this example, we use a lambda expression to filter the elements of a List based on their length.

Can you provide an example of a higher-order function using lambda expressions?

Example:

public class Example {

public static void main(String[] args) {

Function<Integer, Integer> square = x -> x * x;

System.out.println(square.apply(4)); // prints 16

}

}

In this example, we define a higher-order function square that takes an integer as input and returns its square. We then apply the lambda expression to the value 4.

How do you use lambda expressions with method references?

Example:

public class Example {

public static void main(String[] args) {

BiConsumer<String, Integer> biConsumer = String::concat;

biConsumer.accept("Hello, ", 25); // prints "Hello, 25"

}

}

In this example, we use a lambda expression to refer to the concat() method of the String class.

Can you explain how lambda expressions can be used with Java 8's Stream API?

Answer: Lambda expressions are commonly used as part of Java 8's Stream API to create intermediate operations that transform data. For example, using map(), filter(), or reduce() methods on a stream to perform specific operations.

How do you use lambda expressions with recursion?

Example:

public class Example {

public static void main(String[] args) {

RecursiveFunction rf = x -> x == 0 ? 1 : Math.min(x, recursiveFunction(x - 1));

System.out.println(rf.apply(5)); // prints the smallest number that can be expressed as a sum of 2's and 5's using up to 5 iterations

}

}

In this example, we define a lambda expression that implements a recursive function to calculate the smallest number that can be expressed as a sum of 2's and 5's using up to x iterations.

These are just a few examples of lambda expressions in Java. Mastering these concepts will help you write more concise, expressive, and efficient code!

lambda expression in java 8

Java 8 introduced a new feature called Lambda Expressions, which allows you to write concise and expressive code using small anonymous functions.

A lambda expression is a function that can be defined inline within your Java code. It's similar to an anonymous function in functional programming languages like Haskell or Python. A lambda expression has two main parts: the parameter list and the body.

Here's a simple example of how you might use a lambda expression:

// Define a lambda expression

Runnable r = () -> System.out.println("Hello, World!");

// Run the lambda expression

r.run();

In this example, we define a Runnable interface that has a single method run(). We then create a lambda expression that takes no parameters and prints "Hello, World!" to the console. The lambda expression is assigned to the r variable, which can be thought of as an anonymous instance of the Runnable interface.

One of the main benefits of lambda expressions in Java 8 is that they allow you to simplify your code by removing the need for inner classes and interfaces. For example:

// Old way (Java 7 or earlier)

List names = Arrays.asList("John", "Mary", "Jane");

Collections.sort(names, new Comparator() {

public int compare(String s1, String s2) {

return s1.compareTo(s2);

}

});

// New way (Java 8)

List names = Arrays.asList("John", "Mary", "Jane");

names.sort((s1, s2) -> s1.compareTo(s2));

In this example, we're sorting a list of strings using the Collections.sort() method. In Java 7 or earlier, you would have to define an inner class that implements the Comparator interface, like this:

new Comparator() {

public int compare(String s1, String s2) {

return s1.compareTo(s2);

}

}

But in Java 8, you can use a lambda expression to simplify your code and make it more readable.

Lambda expressions also allow you to pass functions as arguments to methods. For example:

// Old way (Java 7 or earlier)

List names = Arrays.asList("John", "Mary", "Jane");

Collections.sort(names, new Comparator() {

public int compare(String s1, String s2) {

return s1.compareTo(s2);

}

});

// New way (Java 8)

List names = Arrays.asList("John", "Mary", "Jane");

names.sort((s1, s2) -> s1.compareTo(s2));

In this example, we're passing a Comparator interface to the sort() method. In Java 7 or earlier, you would have to define an inner class that implements the Comparator interface, like this:

new Comparator() {

public int compare(String s1, String s2) {

return s1.compareTo(s2);

}

}

But in Java 8, you can use a lambda expression to simplify your code and make it more readable.

Lambda expressions also allow you to create higher-order functions, which are functions that take other functions as arguments. For example:

// Define a function that takes another function as an argument

public static void process(IntFunction f) {

System.out.println(f.applyAsInt(42));

}

// Use the function with a lambda expression

process(x -> x + 1);

In this example, we define a process() method that takes a IntFunction interface as an argument. We then use the process() method with a lambda expression that adds 1 to its input.

Overall, lambda expressions in Java 8 provide a powerful and expressive way to simplify your code and make it more readable. They allow you to define small anonymous functions inline within your Java code, which can be useful for creating one-off functions or simplifying complex logic.