{"id":79193,"date":"2020-06-30T09:00:28","date_gmt":"2020-06-30T03:30:28","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=79193"},"modified":"2020-06-30T09:00:28","modified_gmt":"2020-06-30T03:30:28","slug":"functional-interface-in-java","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/functional-interface-in-java\/","title":{"rendered":"Functional Interface in Java"},"content":{"rendered":"<p>In our last article, we discussed the Reflection API in Java. In this article, we will discuss the Functional interface in Java and @FunctionalInterface Annotation in Java. Within@FunctionalInterface Annotation, we will discuss the Java.util.function package with examples.<\/p>\n<p>We will also discuss Lambda Expression in the Java programming language.<\/p>\n<h3>Functional Interface in Java<\/h3>\n<p>A functional interface is an interface in which there is only one abstract method. A functional interface has only one functionality to exhibit. From Java 8 onwards, we can use lambda expressions to represent the instance of a functional interface.<\/p>\n<p>There can be any number of default and static methods in a functional interface that have an implementation. Some of the examples of functional interfaces in Java are Runnable, ActionListener, Comparable interfaces.<\/p>\n<p>We had to use anonymous inner class objects to implement functional interfaces before Java 8.<\/p>\n<h3>Rules of defining a Functional Interface<\/h3>\n<p>The functional interface must have only one abstract method. Along with the one abstract method, they can have any number of default and static methods.<\/p>\n<h3>Examples of Functional Interface<\/h3>\n<p>The example of functional interface example in Java is:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public interface MyFunctionalInterface {\n  public void mymethod();\n}<\/pre>\n<p>The above example shows a functional interface because it only contains a single method that has no implementation.<\/p>\n<p>Following code snippet is another example of a Java functional interface that has an implementation of some methods:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public interface MyFunctionalInterface {\n  public void myMethod();\n\n  public default void printText(String text) {\n    System.out.println(text);\n  }\n\n  public static void printText(String text, PrintWriter writer) throws IOException {\n    writer.write(text);\n  }\n}<\/pre>\n<p>The above interface is a functional interface because it only contains a single non-implemented method.<\/p>\n<h3>Implementation of Functional Interfaces by a Lambda Expression in Java<\/h3>\n<p>Lambda Expressions in Java are the way through which we can visualize functional programming in the object-oriented world. Objects are the basic building block of Java programming language and we can never use a function without an object.<\/p>\n<p>Therefore, Java provides support for using lambda expressions only with functional interfaces. We can implement a Java functional interface using Java Lambda Expression.<\/p>\n<h4>Examples of Java Lambda Expressions<\/h4>\n<p>Below are some code snippets for lambda expressions with small comments explaining them.<\/p>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400\">() -&gt; {}\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">No parameters; void result<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">() -&gt; 42\u00a0\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">No parameters, expression body<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\u00a0 () -&gt; null\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">No parameters, expression body<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\"> () -&gt; { return 42; }\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">No parameters, block body with return<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\"> () -&gt; { System.gc(); }\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">No parameters, void block body<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400\">(int x) -&gt; x+1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">Single declared-type argument<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">(int x) -&gt; { return x+1; }<\/span><\/td>\n<td><span style=\"font-weight: 400\">same as above<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">(x) -&gt; x+1\u00a0\u00a0\u00a0\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">Single inferred-type argument, same as below<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\u00a0 x -&gt; x+1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">Parentheses optional for single inferred-type case<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400\">(String s) -&gt; s.length()\u00a0\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">Single declared-type argument<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\"> (Thread t) -&gt; { t.start(); }\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">Single declared-type argument<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">s -&gt; s.length()\u00a0\u00a0\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">Single inferred-type argument<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">t -&gt; { t.start(); }\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">Single inferred-type argument<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400\">(int x, int y) -&gt; x+y\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">Multiple declared-type parameters<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">(x,y) -&gt; x+y\u00a0\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">Multiple inferred-type parameters<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">(x, final y) -&gt; x+y\u00a0\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">Illegal: can&#8217;t modify inferred-type parameters<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">(x, int y) -&gt; x+y\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">Illegal: can&#8217;t mix inferred and declared types<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Program to Implement Java Functional Interface using Lambda Expressions<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\/\/Java program to implement the functional interface using lambda expressions\npublic class Example {\n  public static void main(String args[]) {\n    \/\/lambda expression to create the object\n    new Thread(() - &gt;{\n      System.out.println(\"Created a new thread\");\n    }).start();\n  }\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Created a new thread<\/div>\n<h4>Why do we need Lambda Expression?<\/h4>\n<p>We need to use Lambda Expressions due to following reasons:<\/p>\n<h5>1. Reduced Lines of Code<\/h5>\n<p>Reduced lines of code is one of the clear benefits of using a lambda expression. We can create instances of a functional interface using lambda expression rather than using an anonymous class.<\/p>\n<h5>2. Sequential and Parallel Execution Support<\/h5>\n<p>Another advantage of using lambda expression is that we can get a Stream API sequential and parallel execution support.<\/p>\n<h5>3. Passing Behaviors into methods<\/h5>\n<p>We can use lambda expressions to pass the behavior of a method. Let\u2019s understand this with a simple example. Suppose, we have to write a method to sum the numbers in a list if they match the given condition.<\/p>\n<p>We can use the Predicate interface and write a method like below.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public static int sumWithCondition(List &lt; Integer &gt; numbers, Predicate &lt; Integer &gt; predicate) { \n    return numbers.parallelStream().filter(predicate).mapToInt(i - &gt;i).sum();\n}<\/pre>\n<p><strong>For example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\/\/sum of all numbers\nsumWithCondition(numbers, n - &gt;true)\n\/\/sum of all even numbers\nsumWithCondition(numbers, i - &gt;i % 2 == 0)\n\/\/sum of all numbers greater than 5\nsumWithCondition(numbers, i - &gt;i &gt; 5)<\/pre>\n<h5>4. Higher Efficiency with Laziness<\/h5>\n<p>Lambda expressions provide a lazy evaluation with higher efficiency. For example, suppose, we have to write a method to find out the maximum odd number in the range 4 to 15 and return the square of it. Using Lambda expression, we can write the code like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public static int findSquareOfMaxOdd(List &lt; Integer &gt; numbers) {\n  return numbers.stream().filter(NumberTest::isOdd)\n  \/\/Predicate is functional interface and\n  .filter(NumberTest::isGreaterThan4)\n  \/\/ we are using lambdas to initialize it\n  .filter(NumberTest::isLessThan15)\n  \/\/ rather than anonymous inner classes\n  .max(Comparator.naturalOrder()).map(i - &gt;i * i).get();\n}\npublic static boolean isOdd(int i) {\n  return i % 2 != 0;\n}\npublic static boolean isGreaterThan4(int i) {\n  return i &gt; 4;\n}\npublic static boolean isLessThan15(int i) {\n  return i &lt; 15;\n}<\/pre>\n<h3>@FunctionalInterface Annotation<\/h3>\n<p>We can use the @FunctionalInterface annotation to ensure that there is not more than one abstract method in a functional interface.<\/p>\n<p>In case if there is more than one abstract method in the functional interface, the compiler gives an \u2018Unexpected @FunctionalInterface annotation\u2019 message. However, there is no compulsion to use this annotation.<\/p>\n<p><strong>Code to understand @FuctionalInterface Annotation in Java:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\/\/Program to implement a user-defined functional interface using lambda expressions\n@FunctionalInterface\ninterface Square {\n  int calculateSquare(int x);\n}\npublic class Test {\n  public static void main(String args[]) {\n    int num = 10;\n\n    \/\/lambda expression to define the calculate method\n    Square sq = (int x) - &gt;x * x;\n    int answer = sq.calculateSquare(num);\n    System.out.println(\"The square of the number is: \" + answer);\n  }\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">The square of the number is: 100<\/div>\n<h3>Primitive Function Specializations<\/h3>\n<p>We know that primitive types cannot be a generic type argument. Therefore, there are versions of the Functional interface for some primitive types such as double, int, long, and their combinations in return types and argument:<\/p>\n<ul>\n<li><strong>IntFunction, LongFunction, DoubleFunction:<\/strong> They take arguments of the specified type, and their return type is parameterized.<\/li>\n<li><strong>ToIntFunction, ToLongFunction, ToDoubleFunction:<\/strong> They take parameterized arguments and the return type is of specified type.<\/li>\n<li><strong>DoubleToIntFunction, DoubleToLongFunction, IntToDoubleFunction, IntToLongFunction, LongToIntFunction, LongToDoubleFunction:<\/strong> These have both argument and return type as primitive types, as specified by their names.<\/li>\n<\/ul>\n<h3>Two-Arity Function Specializations<\/h3>\n<p>We have to use additional interfaces to define lambdas with two arguments. They contain the \u201cBi\u201d keyword in their names, for example, BiFunction, ToDoubleBiFunction, ToIntBiFunction, and ToLongBiFunction.<\/p>\n<p>One of the examples of using this interface in the standard API is in the Map.replaceAll method. This method allows replacing all values in a map with some calculated value.<\/p>\n<p>Let us see an implementation of a BiFunction. It receives a key and an old value to calculate a new value for the income and returns the value.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Map &lt; String,Integer &gt; income = new HashMap &lt; &gt;();\nincome.put(\"Sam\", 70000);\nincome.put(\"Tina\", 40000);\nincome.put(\"Lisa\", 50000);\n\nincome.replaceAll((name, oldValue) - &gt;name.equals(\"Tina\") ? oldValue: oldValue + 20000);<\/pre>\n<h3>Legacy Functional Interfaces in Java<\/h3>\n<p>Java 8 did not introduce all the functional interfaces. There were many functional interfaces that came from the previous versions of Java and can be used as lambdas.<\/p>\n<p>A significant example is the Runnable and Callable interfaces used in concurrency APIs. Java 8 also marks these interfaces with a @FunctionalInterface annotation.<\/p>\n<h3>Built-in Functional Interfaces in Java<\/h3>\n<p>Java provides a set of functional interfaces designed for common use cases. So there is no need to create your own functional interfaces for every little use case. In this section, we will learn some built-in functional interfaces in Java which are present in the java.util.function package.<\/p>\n<h3>The java.util.function package<\/h3>\n<p>The java.util.function package in Java 8 has many built-in functional interfaces like:<\/p>\n<h4>1. Function<\/h4>\n<p>The Java Function interface or java.util.function.Function interface is one of the most important functional interfaces in Java. The Function interface represents a method that takes a single parameter and returns a single value.<\/p>\n<p>The definition of Function interface looks like:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public interface Function &lt; T,R &gt; {\n  public &lt; R &gt; apply(T parameter);\n}<\/pre>\n<h4>2. Predicate<\/h4>\n<p>The java.util.function.Predicate is a functional interface that represents a simple function. This function takes a single value as a parameter and returns either true or false.<\/p>\n<p>The definition of Predicate functional interface looks like:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public interface Predicate {\n  boolean test(T t);\n}<\/pre>\n<p><strong>Code to implement the Predicate Interface in Java:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\/\/ A program to demonstrate the use of predicate interface\nimport java.util. * ;\nimport java.util.function.Predicate;\nclass Test {\n  public static void main(String args[]) {\n    \/\/create a list of strings\n    List &lt; String &gt; names = Arrays.asList(\"Tech\", \"Vidvan\", \"Java\", \"Tutorials\", \"TV2\");\n\n    \/*declare the predicate type as a string and use a lambda expression to create object *\/\n    Predicate &lt; String &gt; p = (s) - &gt;s.startsWith(\"T\");\n\n    \/\/ Iterate through the list\n    for (String st: names) {\n      \/\/ call the test method\n      if (p.test(st)) System.out.println(st);\n    }\n  }\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Tech<br \/>\nTutorials<br \/>\nTV2<\/div>\n<h4>3. Unary Operator<\/h4>\n<p>The java.util.function.UnaryOperation interface in Java represents an operation. This operation takes a single parameter and returns a parameter of the same type.<\/p>\n<p>Below is the example of a Java UnaryOperator implementation:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">UnaryOperator &lt; Person &gt; unaryOperator = (person) - &gt;{\n  person.name = \"New Name\";\n  return person;\n};<\/pre>\n<h4>4. Binary Operator<\/h4>\n<p>Java Binary Operator interface represents an operation that takes two parameters and returns a single value. Both parameters and the return type of the function must be of the same type.<\/p>\n<p>The Java BinaryOperator interface is useful when there is a need to implement functions like sum, subtract, divide, multiply, etc., that have two elements of the same type, and return a third element of the same type.<\/p>\n<p>Below is an example of implementation of the BinaryOperator interface:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">BinaryOperator &lt; MyValue &gt; binaryOperator = (value1, value2) - &gt;{\n  value1.add(value2);\n  return value1;\n};<\/pre>\n<h4>5. Supplier<\/h4>\n<p>The Supplier interface of Java is a functional interface that represents a function that supplies a value of some sort. This interface can also be thought of as a factory interface.<\/p>\n<p>Below is an example of implementation of the Java Supplier interface:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Supplier &lt; Integer &gt; supplier = () - &gt;new Integer((int)(Math.random() * 1000D));<\/pre>\n<h4>6. Consumer<\/h4>\n<p>The Consumer interface of java.util\/function package is a functional interface that represents a function that takes a value without returning any value. The implementation of Java Consumer interface can be used in printing out some values or writing a value to a file, or over the network, etc.<\/p>\n<p>Below is an example of implementation of the Java Consumer interface:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Consumer &lt; Integer &gt; consumer = (value) - &gt;System.out.println(value);<\/pre>\n<h3>Functional interface example: using anonymous inner class vs using a lambda expression<\/h3>\n<p>We have been using functional interfaces even before the introduction of Java 8. We used them by creating anonymous inner classes using functional interfaces. The functional interfaces such as Runnable, ActionListener, Comparator, etc. have a single abstract method.<\/p>\n<p>Let us see an example of ActionListener. We will compare how we used ActionListener using Anonymous inner class before Java 8 and how we can implement it using lambda expressions.<\/p>\n<p><strong>ActionListener Example Using an anonymous inner class: Before Java 8:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import javax.swing. * ;\nimport java.awt. * ;\nimport java.awt.event. * ;\nclass Example extends JFrame {\n  JButton button;\n  public Example() {\n    setTitle(\"Button Action Example without Lambda Expression\");\n    setSize(400, 300);\n    setVisible(true);\n    setLayout(new FlowLayout());\n    setDefaultCloseOperation(EXIT_ON_CLOSE);\n\n    button = new JButton(\"Button\");\n    button.setBounds(100, 100, 90, 40);\n    button.addActionListener(new ActionListener() {\n      public void actionPerformed(ActionEvent e) {\n        System.out.println(\"You clicked the button.\");\n      }\n    });\n    add(button);\n  }\n  public static void main(String args[]) {\n    new Example();\n  }\n}<\/pre>\n<p><strong>ActionListener Example: Lambda Expression: From Java 8:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import javax.swing. * ;\nimport java.awt. * ;\nclass Example extends JFrame {\n  JButton button;\n  public Example() {\n    setTitle(\"Button Action Example using Lambda Expression\");\n    setSize(400, 300);\n    setVisible(true);\n    setLayout(new FlowLayout());\n    setDefaultCloseOperation(EXIT_ON_CLOSE);\n\n    button = new JButton(\"Button\");\n    button.setBounds(100, 100, 90, 40);\n\n    \/\/Lambda expression\n    button.addActionListener(e - &gt;System.out.println(\"You clicked the button.\"));\n    add(button);\n  }\n  public static void main(String args[]) {\n    new Example();\n  }\n}<\/pre>\n<p><strong>Output for both the programs is the same:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/06\/pasted-image-0-19.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-79257\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/06\/pasted-image-0-19.png\" alt=\"Java functional interface\" width=\"400\" height=\"300\" \/><\/a><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/06\/pasted-image-0-1-8.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-79258\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/06\/pasted-image-0-1-8.png\" alt=\"java functional interface\" width=\"677\" height=\"342\" \/><\/a><\/p>\n<h3>Important Points\/Observations<\/h3>\n<p><strong>1.<\/strong> There can be only one abstract method in a functional interface but there can be multiple default methods in it.<\/p>\n<p><strong>2.<\/strong> @FunctionalInterface annotation ensures that a functional interface cannot have more than one abstract method. The use of this annotation is not mandatory.<\/p>\n<p><strong>3.<\/strong> There are many built-in functional interfaces in the java.util.function package<\/p>\n<h3>Conclusion<\/h3>\n<p>Hence, in this Java Functional Interface tutorial, we learned about what functional interfaces are in Java with example.<\/p>\n<p>We also discussed Java Lambda expressions and @FunctionalInterface Annotation in Java, in which we covered java.util.function Package. We also saw how Lambda expressions are beneficial for efficient coding.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our last article, we discussed the Reflection API in Java. In this article, we will discuss the Functional interface in Java and @FunctionalInterface Annotation in Java. Within@FunctionalInterface Annotation, we will discuss the Java.util.function&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":79244,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[2938,2939,2940,2941],"class_list":["post-79193","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-functional-interface-in-java","tag-interface-of-java","tag-java-functional-interface","tag-lambda-expression-in-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Functional Interface in Java - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn Functional interface in java, their implementation using lambda expression, Built-in Functional Interfaces in Java, @FunctionalInterface Annotation\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/functional-interface-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Functional Interface in Java - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn Functional interface in java, their implementation using lambda expression, Built-in Functional Interfaces in Java, @FunctionalInterface Annotation\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/functional-interface-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-06-30T03:30:28+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/Functional-interface-in-java-tv.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Functional Interface in Java - TechVidvan","description":"Learn Functional interface in java, their implementation using lambda expression, Built-in Functional Interfaces in Java, @FunctionalInterface Annotation","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/functional-interface-in-java\/","og_locale":"en_US","og_type":"article","og_title":"Functional Interface in Java - TechVidvan","og_description":"Learn Functional interface in java, their implementation using lambda expression, Built-in Functional Interfaces in Java, @FunctionalInterface Annotation","og_url":"https:\/\/techvidvan.com\/tutorials\/functional-interface-in-java\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-06-30T03:30:28+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/Functional-interface-in-java-tv.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/functional-interface-in-java\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/functional-interface-in-java\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Functional Interface in Java","datePublished":"2020-06-30T03:30:28+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/functional-interface-in-java\/"},"wordCount":1580,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/functional-interface-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/Functional-interface-in-java-tv.jpg","keywords":["Functional Interface in Java","interface of java","java functional interface","Lambda expression in java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/functional-interface-in-java\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/functional-interface-in-java\/","url":"https:\/\/techvidvan.com\/tutorials\/functional-interface-in-java\/","name":"Functional Interface in Java - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/functional-interface-in-java\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/functional-interface-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/Functional-interface-in-java-tv.jpg","datePublished":"2020-06-30T03:30:28+00:00","description":"Learn Functional interface in java, their implementation using lambda expression, Built-in Functional Interfaces in Java, @FunctionalInterface Annotation","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/functional-interface-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/functional-interface-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/functional-interface-in-java\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/Functional-interface-in-java-tv.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/Functional-interface-in-java-tv.jpg","width":1200,"height":628,"caption":"Functional interface in java"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/functional-interface-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Functional Interface in Java"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/79193","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=79193"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/79193\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/79244"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=79193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=79193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=79193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}