Functional programming is a paradigm that allows programming using expressions i.e. declaring functions, passing functions as arguments and using functions as statements.
- Print each element from list in its own line
- Print courses, courses having name with Spring, courses having name greater than 3 characters
- Print Square of each number in the list
- Print sum of all elements in the list
- Print max and min number from all elements in the list
- Square every number in the list and find the sum of squares
- Cube every number in the list and find the sum of cubes
- Find sum of odd numbers in the list
- Print distinct numbers in the list
- Sort list of numbers and print it
- Sort list of strings and print it
- Square each element of list and collect it as new list
- Find all even element of list and collect it as new list
- Find length of each course element of list and collect it as new list
- Print each element from list in its own line
- Print courses, courses having name with Spring, courses having name greater than 3 characters
- Print sum of all elements in the list
- Print max and min number from all elements in the list
- Square every number in a list and find the sum of squares
- Cube every number in a list and find the sum of cubes
- Find sum of odd numbers in a list
- Print distinct numbers in the list
- Sort list of numbers and print it
- Sort list of strings and print it
- Square each element of list and collect it as new list
- Find all even element of list and collect it as new list
- Find length of each course element of list and collect it as new list
- Function or method which returns another stream of element called as Intermediate functions or methods or operations. They are Lazy and execute only when terminal operation is executed.
- Function or method which does not return another stream of element called as Terminal functions or methods or operations.
- Example
A functional interface has exactly one abstract method.
- Predicate<T>: Represents a predicate (boolean-valued function) of one argument.
- Function<T, R>: Represents a function that accepts one argument and produces a result.
- Consumer<T>: Represents an operation that accepts a single input argument and returns no result.
- BinaryOperator<T>: Represents an operation upon two operands of the same type, producing a result of the same type as the operands.
- Supplier<T>: Represents a supplier of results. It does not take any arguments.
- UnaryOperatory<T>: Represents an operation on a single operand that produces a result of the same type as its operand.
- BiPredicate<T, R>: Represents a predicate (boolean-valued function) of two arguments.
- BiFunction<T, U, R>: Represents a function that accepts two arguments and produces a result.
- BiConsumer<T, U>: Represents an operation that accepts two input arguments and returns no result.
- IntBinaryOperator: Represents an operation upon two int-valued operands and producing an int-valued result.
- IntConsumer: Represents an operation that accepts a single int-valued argument and returns no result.
- IntFunction<R>: Represents a function that accepts an int-valued argument and produces a result.
- IntPredicate: Represents a predicate (boolean-valued function) of one int-valued argument.
- IntSupplier: Represents a supplier of int-valued results.
- IntToDoubleFunction: Represents a function that accepts an int-valued argument and produces a double-valued result.
- IntToLongFunction: Represents a function that accepts an int-valued argument and produces a long-valued result.
- IntUnaryOperator: Represents an operation on a single int-valued operand that produces an int-valued result.
Passing the behaviour as parameter or argument to a method is called as Behavior Parameterization.
Method reference is used to refer method of functional interface. The Method reference can only be used to replace a single method of lambda expression.
Higher Order Function is a function which returns a function.
Few examples of real time Java problems that made very easy using functional programming