Functional Programming
Lambda Expression
- It were introduced in Java 8.
- Lambda expression is an anonymous function. It’s a function without name and does not belongs to any class.
- Lambda expression is mainly used to implement functional interfaces.
Functional Interfaces
- Introduced in Java 8.
- An interface that contains exactly one abstract method is known as functional interface.
- Functional interface can have any number of default , static methods but can contain only one abstract method.
- We can use Java 8 provides the @FunctionalInterface annotation to mark an interface as a functional interface.
Lambda vs Method
- Method is always belong to class or object in Java where as Lambda does not belongs to any class or object.
- Method has the main parts:
- Name
- Parameter List
- Body
- Return Type - Lambda expression in Java has these main parts:
- No Name — since it is anonymous function so no need to have a name
- Parameter List
- Body
- No Return Type
- ( ) -> Lambda Input Parameters
- → : Denoting Lambda
- { } → Lambda Body
Predefined Functional Interfaces
- Function functional interface input one and output is one. Has one method as apply.
- Consumer — input one output is void . Has one accept method
- Supplier — Generic interface it doesnt take any input and returns only output. Has one method as get method.
Method References
- Java provides a new feature called method reference in Java 8.
- Method reference is used to refer method of the functional interface. It is a compact and easy form of lambda expression.
- Each time when you are using a lambda expression to just referring a method, you can replace your lambda expression with a method reference.
- Function<Integer,Double> function=(input) -> Math.sqrt(input); [Lambda Function]
- Function<Integer, Double> functionMethodRef = Math::sqrt; [Method reference)
Get Yunus Iqbal Siddique’s stories in your inbox
Join Medium for free to get updates from this writer.
Types of Method Reference
There are four types of method references in Java:
- Method reference to static method: its syntax is Class::StaticMethodName
Function<Integer, Double> functionMethodRef = Math::sqrt;
sqrt is a static method - Reference to an instance method of a particular object : the syntax is Object::instanceMethodName
MethodReferencesDemo methodReferencesDemo = new MethodReferences Demo();
Printable printableMethodRef = methodReferencesDemo::display; - Reference to an instance method of an arbitrary object of specific type: the syntax is Class::instanceMethodName.
Function<String,String> stringMethodRef= String::toLowerCase; - Reference to a constructor: its syntax is ClassName::new
Function<List<String>, Set<String>> setFunctionMethodRef = HashSet::new;
Runnable Interface
- It is basically a functional interface
Optional Class
- A container object which may or may not contain a non null value. If a value is present , isPresent() will return true and get() will return th value.
- Additional methods that depends on the presence or absence of a contained value are provided, such as orElse() (return a default value if value not present) and ifPresent() (execute a block of code if the value is present).
- This is a value based class, use of identity sensitive operations (including reference equality (==), identity hash code, or synchronization) on instane of Optional may have unpredictable results and should be avoided.
- Optional.ofNullable(x) → Here x can be null also
Default Methods
- Now in interface we can have default methods
exp: default String exp(){ return null;}
Difference Between Collections and Streams
- Collections → Collections are used to store and group the data in a particular data structure like list, Set or Map.
Streams → Streams are used to perform complex data processing operations like filtering , matching, mapping stored data such as arrays, collections or I/O resources. - Collections → You can add or remove elements from collections.
Streams → You can’t add or remove elements from streams. - Collections → It has to be iterated externally.
Streams → They are internally iterated - Collections → It can be traversed multiple times.
Streams → They are traversable only once. - Collections → They are eagerly constructed.
Streams → They are lazily constructed.








