How to Use Java Lambda Expressions (Anonymous Functions)?
Java Lambda expressions, aka anonymous functions, allow you to pass a block of code as a parameter.
In this way, you can write shorter, cleaner and more functional code.
🧠 Technical Summary
Main topic: Lambda expressions (anonymous functions) in Java
Purpose: Simplify code and increase readability by reducing unnecessary class definitions
Steps:
- Learning the lambda expression structure
- Experiment with examples on JShell
- Implementing built-in functional interfaces (Predicate, Consumer, Function...)
💡 Lambda expressions support Java's functional programming approach and work integrated with the
java.util.functionpackage.
⚙️ 1. Understanding Lambda Structure
Lambda general syntax:
(argümanlar) -> { gövde }
Arguments: Can be written in parentheses, without specifying the type (Java automatically detects it).
Arrow Sign (->): Connects the parameters to the code block.
Body: Contains the block of code to be executed.
💬 To test the examples using JShell, type jshell in the terminal and use the /exit command to exit.
🐾 2. Simple Lambda Example
Let's create a list:
List<String> evcilHayvanlar = Arrays.asList("Köpek", "Kedi");
Let's print each item to the screen:
evcilHayvanlar.forEach(x -> System.out.println(x));
Simple version of the same code (method reference):
evcilHayvanlar.forEach(System.out::println);
💡 System.out::println is short for x -> System.out.println(x).
📜 3. Multi-Line Lambda Body
Used if the lambda body contains more than one line:
evcilHayvanlar.forEach(x -> {
System.out.println("Adı: " + x);
System.out.println("Sıra No: " + evcilHayvanlar.indexOf(x));
});
Output:
Adı: Köpek
Sıra No: 0
Adı: Kedi
Sıra No: 1
🧩 4. Built-in Functional Interfaces
The java.util.function package that comes with Java 8 provides ready-made interfaces for frequently used Lambda scenarios. These contain a single method and are directly compatible with Lambda expressions.
✅ Predicate (Condition Check)
Tests a condition, returns boolean.
Predicate<String> filtreleK = x -> x.startsWith("K");
evcilHayvanlar.stream().filter(filtreleK).forEach(System.out::println);
Output: Dog, Cat
📦 Consumer
It uses the value but does not return it (void is returned).
Consumer<String> hayvanYazdir = x -> System.out.println(x);
evcilHayvanlar.forEach(hayvanYazdir);
Output: Dog, Cat
🔄 Function (Value Conversion)
Converts one value to another value.
Function<String, String> buyukHarfYap = x -> x.toUpperCase();
evcilHayvanlar.stream().map(buyukHarfYap).forEach(System.out::println);
Output: DOG, CAT
⏰ Supplier (Value Provider)
It doesn't take any parameters, it produces a value when called.
Supplier<java.time.LocalTime> saatVer = () -> java.time.LocalTime.now();
System.out.println(saatVer.get());
Output: instant time like 21:52:38.384278
🔁 UnaryOperator (Single Operation)
Input and output type are the same, usually used in conversion.
UnaryOperator<String> buyukHarf = x -> x.toUpperCase();
System.out.println(buyukHarf.apply("kopek"));
Output: COPEK
➕ BinaryOperator (Binary Operation)
It combines two values, input and output type are the same.
BinaryOperator<Integer> toplama = (x, y) -> x + y;
System.out.println(toplama.apply(5, 3));
Output: 8
❓ Frequently Asked Questions (FAQ)
- Why are Lambda expressions anonymous?
Because they don't have names. They are written for one-time transactions.
- What does method reference (::) do?
If Lambda calls only one method, this statement shortens the code: System.out::println.
- Can all interfaces be Lambda?
No. There can be functional interfaces (@FunctionalInterface) that contain only a single abstract method.
- What is the difference between Predicate and Function?
Predicate returns only boolean; Function releases the return type.
- Where are Lambda expressions used?
Stream API is common in scenarios such as event management, filtering, and data processing.
🏁 Conclusion
In this guide, you learned how to create Lambda expressions in Java, use method references and built-in interfaces (Predicate, Consumer, Function, etc.) in the java.util.function package.
Now you can write your codes with a shorter, readable and functional approach. 💡 You can test your own examples in the Java environment on GenixNode and run your codes instantly.

