Basic Syntax
(parameters) -> { statements; }
Examples
Using a for-loop
import java.util.ArrayList;
import java.util.List;
public class LambdaExample {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
names.add("Charlie");
// Using lambda expression without curly brackets
names.forEach(name -> System.out.println(name));
}
}
Simple math calculations
// With curly brackets and return statement
MyMathOperation add = (a, b) -> {
return a + b;
};
// Without curly brackets and return statement
MyMathOperation addSimple = (a, b) -> a + b;
Java library that helps managing sequences of elements.
filter: filters out elements based on a certain condition
collect: put elements together into a list
💡Collectors is a utility class provided by the Java Standard Library, specifically in the java.util.stream package. The Collectors class provides various static methods that can be used to accumulate the elements of a stream into a collection, or perform other types of reduction operations.💡
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class StreamExample {
public static void main(String[] args) {
// Create a list of integers
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// Use stream to filter even numbers and collect them into a new list
List<Integer> evenNumbers = numbers.stream()
// Intermediate operation: filter even numbers
.filter(n -> n % 2 == 0)
// Terminal operation: collect to a list
.collect(Collectors.toList());
System.out.println("Even Numbers: " + evenNumbers);
}
}
map: Transforms elements. For example, making all names all caps.
forEach: Performs an action for each element of the stream
import java.util.Arrays;
import java.util.List;
public class StreamOperationsExample {
public static void main(String[] args) {
List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David");
// Use stream to convert all names to uppercase and print them
names.stream()
// Intermediate operation: convert each name to uppercase
.map(String::toUpperCase)
// Terminal operation: print each name
.forEach(System.out::println);
}
}
Output
ALICE
BOB
CHARLIE
DAVID
reduce:
The reduce method takes two parameters:
The reduce operation applies the binary operator iteratively to all elements of the stream, accumulating the result. The final result of the reduction is the sum of all elements, which is stored in the sum variable.
int sum = numbers.stream()
.reduce(0, Integer::sum);
Another example
import java.util.Arrays;
import java.util.List;
public class StreamReduceStringExample {
public static void main(String[] args) {
// Create a list of strings
List<String> words = Arrays.asList("Hello", "World", "Java", "Streams");
// Use reduce to concatenate all strings
String concatenatedString = words.stream()
.reduce("", (a, b) -> a + " " + b);
// Print the result
System.out.println("Concatenated String: " + concatenatedString.trim());
// Output: "Hello World Java Streams"
}
}
💡 Why is .trim() is Needed? 💡
In .reduce(), the initial concatenation is with an empty string("") and the word "Hello" with a space (" ") between them. Therefore, there is unnecessary white space in the beginning of the concatenatedString.