Java Lambda Expression Coding Problems
Below are some common java lambda expression coding problems and their outputs.These java lambda expression coding problems are designed to test your understanding of core lambda expression concept.Preparing for these java lambda expression coding problems will help you stand out and showcase your skill during technical interviews

1.Filter Even Numbers from a List
import java.util.*;
import java.util.function.Predicate;
public class LambdaExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// Using a lambda expression to filter even numbers
List<Integer> evenNumbers = new ArrayList<>();
numbers.stream()
.filter(n -> n % 2 == 0) // Lambda expression to filter even numbers
.forEach(evenNumbers::add);
System.out.println(“Even numbers: ” + evenNumbers);
}
}
Output:
Even numbers: [2, 4, 6, 8, 10]
2.Sorting a List of Strings by Length
import java.util.*;
public class LambdaExample {
public static void main(String[] args) {
List<String> strings = Arrays.asList(“apple”, “banana”, “kiwi”, “cherry”, “grape”);
// Using a lambda expression to sort by string length
strings.sort((str1, str2) -> Integer.compare(str1.length(), str2.length()));
System.out.println(“Sorted by length: ” + strings);
}
}
Output:
Sorted by length: [kiwi, apple, grape, banana, cherry]
3.Mapping Numbers to Their Squares
import java.util.*;
import java.util.stream.Collectors;
public class LambdaExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
// Using a lambda expression to map each number to its square
List<Integer> squares = numbers.stream()
.map(n -> n * n) // Lambda expression to square each number
.collect(Collectors.toList());
System.out.println(“Squares: ” + squares);
}
}
Output:
Squares: [1, 4, 9, 16, 25]
4.Summing All Numbers in a List
import java.util.*;
public class LambdaExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
// Using a lambda expression to sum all numbers
int sum = numbers.stream()
.reduce(0, (a, b) -> a + b); // Lambda expression to add numbers
System.out.println(“Sum: ” + sum);
}
}
Output:
Sum: 15
5.Checking if Any Element in List is Even
import java.util.*;
public class LambdaExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 3, 5, 7, 9, 10);
// Using a lambda expression to check if any element is even
boolean anyEven = numbers.stream()
.anyMatch(n -> n % 2 == 0); // Lambda expression to check for even numbers
System.out.println(“Any even number present? ” + anyEven);
}
}
Output:
Any even number present? True
6.Finding the Maximun Value in a List
import java.util.*;
public class LambdaExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(10, 20, 4, 45, 99);
// Using a lambda expression to find the maximum value
int max = numbers.stream()
.max((n1, n2) -> Integer.compare(n1, n2)) // Lambda expression to compare numbers
.orElseThrow(NoSuchElementException::new);
System.out.println(“Maximum value: ” + max);
}
}
Output:
Maximum value: 99
7.Counting Odd Numbers in a List
import java.util.*;
public class LambdaExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// Using a lambda expression to count odd numbers
long oddCount = numbers.stream()
.filter(n -> n % 2 != 0) // Lambda expression to filter odd numbers
.count();
System.out.println(“Count of odd numbers: ” + oddCount);
}
}
Output:
Count of odd numbers: 5
8.Converting List of Strings to Uppercase
import java.util.*;
import java.util.stream.Collectors;
public class LambdaExample {
public static void main(String[] args) {
List<String> strings = Arrays.asList(“apple”, “banana”, “kiwi”, “cherry”);
// Using a lambda expression to convert each string to uppercase
List<String> uppercased = strings.stream()
.map(s -> s.toUpperCase()) // Lambda expression to convert to uppercase
.collect(Collectors.toList());
System.out.println(“Uppercase strings: ” + uppercased);
}
}
Output:
Uppercase strings: [APPLE, BANANA, KIWI, CHERRY]
9.Filter Palindromes from a List of Strings
import java.util.*;
import java.util.stream.*;
public class LambdaExample {
public static void main(String[] args) {
List<String> words = Arrays.asList(“level”, “java”, “radar”, “hello”, “madam”);
// Using a lambda expression to filter palindromes
List<String> palindromes = words.stream()
.filter(s -> s.equals(new StringBuilder(s).reverse().toString())) // Lambda to check palindrome
.collect(Collectors.toList());
System.out.println(“Palindromes: ” + palindromes);
}
}
Output:
Palindromes: [level, radar, madam]
10.Remove Duplicates from a List
import java.util.*;
import java.util.stream.*;
public class LambdaExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 3, 4, 5, 5, 6);
// Using a lambda expression to remove duplicates
List<Integer> uniqueNumbers = numbers.stream()
.distinct() // Lambda expression to remove duplicates
.collect(Collectors.toList());
System.out.println(“Unique numbers: ” + uniqueNumbers);
}
}
Output:
Unique numbers: [1, 2, 3, 4, 5, 6]
11.Find the First Even Number in a List
import java.util.*;
public class LambdaExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 3, 5, 7, 8, 9, 10);
// Using a lambda expression to find the first even number
Optional<Integer> firstEven = numbers.stream()
.filter(n -> n % 2 == 0) // Lambda to find even numbers
.findFirst();
firstEven.ifPresent(even -> System.out.println(“First even number: ” + even));
}
}
Output:
First even number: 8
12.Grouping Strings by Length
import java.util.*;
import java.util.stream.*;
public class LambdaExample {
public static void main(String[] args) {
List<String> words = Arrays.asList(“apple”, “banana”, “kiwi”, “grape”, “pear”);
// Using a lambda expression to group strings by length
Map<Integer, List<String>> groupedByLength = words.stream()
.collect(Collectors.groupingBy(String::length)); // Grouping by length
System.out.println(“Grouped by length: ” + groupedByLength);
}
}
Output:
Grouped by length: {4=[kiwi, pear], 5=[apple, grape], 6=[banana]}
13.Check if All Numbers in a List are Positive
import java.util.*;
public class LambdaExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
// Using a lambda expression to check if all numbers are positive
boolean allPositive = numbers.stream()
.allMatch(n -> n > 0); // Lambda expression to check positivity
System.out.println(“All numbers are positive: ” + allPositive);
}
}
Output:
All numbers are positive: true