Lambda expressions in Java are often tested on platforms like HackerRank to assess candidates’ understanding of functional programming concepts. While specific questions may vary, here are some common types of HackerRank questions related to Java lambda expressions:
1. Basic Lambda Syntax:
— Write a lambda expression to sort a list of integers in ascending order.
— Create a lambda function to filter even numbers from a list of integers.
2. Using Lambda with Streams:
— Given a list of strings, use lambda expressions to find strings containing a specific substring.
— Calculate the sum of squares of even numbers in a list using lambda expressions and streams.
3. Functional Interfaces:
— Implement a custom functional interface and use it with lambda expressions to perform mathematical operations.
— Write a lambda function to check if a string is a palindrome.
4. Method References:
— Convert lambda expressions to method references where applicable.
— Implement a Comparator using lambda expressions and method references to sort a list of objects.
5. Advanced Lambda Concepts:
— Use lambda expressions to perform complex filtering and mapping operations on collections.
— Implement custom functional interfaces with multiple parameters and use them in lambda expressions.
These questions typically assess candidates’ ability to write concise and efficient code using lambda expressions, understand functional interfaces, work with streams, and apply functional programming principles to solve real-world problems. Practicing these types of questions on platforms like HackerRank can help developers strengthen their Java skills, particularly in the area of lambda expressions and functional programming.
Here’s a simple Java solution for a HackerRank problem involving lambda expressions:
import java.io.*;
import java.util.*;
interface PerformOperation {
boolean check(int a);
}
class MyMath {
public static boolean checker(PerformOperation p, int num) {
return p.check(num);
}
// Lambda expressions to check if a number is odd, prime, or a palindrome
public PerformOperation isOdd() {
return n -> n % 2 != 0;
}
public PerformOperation isPrime() {
return n -> {
if (n < 2)
return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
};
}
public PerformOperation isPalindrome() {
return n -> {
int reversed = 0, original = n;
while (n != 0) {
int digit = n % 10;
reversed = reversed * 10 + digit;
n /= 10;
}
return original == reversed;
};
}
}
public class Solution {
public static void main(String[] args) throws IOException {
MyMath ob = new MyMath();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
PerformOperation op;
boolean ret = false;
String ans = null;
while (T-- > 0) {
String s = br.readLine().trim();
StringTokenizer st = new StringTokenizer(s);
int ch = Integer.parseInt(st.nextToken());
int num = Integer.parseInt(st.nextToken());
if (ch == 1) {
op = ob.isOdd();
ret = ob.checker(op, num);
ans = (ret) ? "ODD" : "EVEN";
} else if (ch == 2) {
op = ob.isPrime();
ret = ob.checker(op, num);
ans = (ret) ? "PRIME" : "COMPOSITE";
} else if (ch == 3) {
op = ob.isPalindrome();
ret = ob.checker(op, num);
ans = (ret) ? "PALINDROME" : "NOT PALINDROME";
}
System.out.println(ans);
}
}
}
This solution demonstrates the usage of lambda expressions to define functional interfaces and perform operations based on user input.