Skip to main content

JAVA 8 FUNTIONAL INTERFACE

JAVA 8 FUNTIONAL INTERFACE

 Funtional Interface :

    An interface which has only one abstract method can be called a funtional interface.

Comparable , Runnable , Callable all these interfaces has only one abstract method and can be consider as funtional interface.

   How it works :

       Once we will create a interface with one abstract method then java internally predicates the input type and based on the Interface reference it apply the logic mentioned after lambda expression 

  lets consider we have created an interface as below

@FunctionalInterface
public interface FuntionalExample {
public int cal(int a, int b);
}

And below class to test our funtional Interface.

public class FuntionalInterfaceExample {

public static void main(String args[])
{
FuntionalExample addition=(int a, int b) -> a+b;
FuntionalExample subtraction=(int a, int b) -> a-b;
FuntionalExample multiplication=(int a, int b) -> a*b;
FuntionalExample division=(int a, int b) -> a/b;

System.out.println(addition.cal(10,5));
System.out.println(subtraction.cal(10,5));
System.out.println(multiplication.cal(10,5));
System.out.println(division.cal(10,5));
}
}

Output is as below :

 15

5

50

2

Here if you noticed we have created instances of  funtional Interface but the logic after lambda expression has been changed and whenever i am using addition reference to call cal(int a , int b) method its doing addition because after lambda we did a+b while for subtraction it's a-b , for multiplication it's a*b while for division it's a/b.

JAVA 8 FUNTIONAL INERFACES :

There are 3 new java functional interafce has been introduced to help in stream api.

1) Consumer is a funtional interface with only one abstract method void accept(T t), with no return type and an input param of type T. In below code snippet we have created reference of Consumer as per our code choice and using it in stream of String.

Consumer<String> printc=c -> {System.out.println("City name :"+c);};

Stream<String> cities=Stream.of("Pune","Kolkata","Bangalore","Mumbai");

cities.forEach(printc);

Output :

City name :Pune

City name :Kolkata

City name :Bangalore

City name :Mumbai

2) Supplier is another funtional interface with only abstract method T get(T t),  takes an input of Type T and return same type or orElseReturn with default values set.


Stream<String> cities=Stream.of("Pune","Kolkata","Bangalore","Mumbai");

Supplier<String> supplier=() -> "No values found";

cities.filter(c -> c.startsWith("A")).forEach(s -> System.out.println(s)).orElseReturn(supplier);

Output : No values found

3) Predicate is another funtional interface with only abstract method boolean Test(T t),  takes an input of Type T and return a boolean type either TRUE or FALSE.

 

Stream<String> cities=Stream.of("Pune","Kolkata","Bangalore","Mumbai");

Predicate<String> predicate=s -> s.startsWith("P");

cities.filter(predicate).forEach(System.out::println);  

Output : Pune





Comments

Popular posts from this blog

Bubble sort Implementation

Bubble sort  : In bubble sort ,we will select the 1st element and compare with all the remaining element, same process we will continue for all the elements as we are traveling the whole Array 2 times except the element which we have selected to compare with other elements but still it will be consider as n time.    So time complexity for bubble sort will be O(n^2).         space complexity for bubble sort will be O(1). // Bubble Sort class BubbleSort { public static void sort ( int [] array) { int n = array. length ; while ( true ) { boolean swapped = false; for ( int i = 0 ; i < n - 1 ; i++) { if (array[i + 1 ] < array[i]) { swap (array , i , i + 1 ) ; swapped = true; } } if (!swapped) break; } } private static void swap ( int [] array , int i , int j) { int temp = array[i] ;...

Object-Oriented Programming Concept in Java

OOPS( Object-Oriented Programming ) Concept in Java :   As we all know Java is Object Oriented programming language and what exactly it means in simple words to understand can be described as whatever is going to happen by Java , it will be based on some Object.  So next question can be what is Object ? , "Object is the representation or reference of Class to access its properties and use its behaviour ", now next is What is Class in java and answer to this question is "A class in java is the blueprint of Properties and Behaviours of it's own Object" as explained in my previous post  BASIC OVERVIEW OF JAVA  (SESSION 1)   Let's understand through an example : public class FirstJavaProgram { int firstNumber=10; int secondNumber=20;      public int sum(int fNum, int sNum){         return fNum+sNum;     }     public static void main(String[] args) {     //our logics ...

JAVA MEMORY LEAK, UTILISATION AND MONITORING USING JFR using Mission Control

JAVA MEMORY LEAK, UTILISATION AND MONITORING USING JFR using Mission Control Java flight recording(JFR) help us to analyse and find the root cause of any slowness in our program along with CPU usage , hot methods and garbage collection , profiling etc. To visualise we need to feed .jfr file to JDK mission control present in JDK bin folder. After successful compilation , we should run the program with below option which will generate the .jfr and feed to mission control.   command :  j ava -XX:+UnlockCommercialFeatures -XX:+FlightRecorder  -XX:StartFlightRecording=duration=200s,filename=flight.jfr -cp ./out/ path-and-class-name Below are some example to understand how this JFR can be helpful. 1. Lets consider we have created a java program in which we have used LinkList to store the elements and in same program we are using contains method inside a for loop of 1 million , in this case each time this contains method will be called then 1 million records will be sc...