<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:media="http://search.yahoo.com/mrss/">

<channel>
	<title>functional interface Archives - Manish Sanger</title>
	<atom:link href="https://www.manishsanger.com/tag/functional-interface/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.manishsanger.com/tag/functional-interface/</link>
	<description>Java, J2EE, Spring Framework, application security,big data,database,design patterns,hibernate,j2ee,java,jdbc,micro-services,mongodb,multithreading,nosql</description>
	<lastBuildDate>Tue, 06 Feb 2018 06:58:07 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.3</generator>
	<item>
		<title>Predicate &#8211; Functional Interface (Java 8)</title>
		<link>https://www.manishsanger.com/predicate/</link>
					<comments>https://www.manishsanger.com/predicate/#respond</comments>
		
		<dc:creator><![CDATA[manishsanger]]></dc:creator>
		<pubDate>Thu, 04 Jan 2018 12:10:08 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[java 8]]></category>
		<category><![CDATA[lambda]]></category>
		<category><![CDATA[functional interface]]></category>
		<category><![CDATA[predicate]]></category>
		<guid isPermaLink="false">http://www.manishsanger.com/?p=287</guid>

					<description><![CDATA[<p>In mathematical logic, a predicate is commonly understood to be a Boolean-valued function P: X→ {true, false}, called the predicate on X. Informally, a predicate is a statement that may be true or false depending on the values of its variables. It can be thought of as an operator or function that returns a value [&#8230;]</p>
<p>The post <a href="https://www.manishsanger.com/predicate/">Predicate &#8211; Functional Interface (Java 8)</a> appeared first on <a href="https://www.manishsanger.com">Manish Sanger</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In mathematical logic, a <strong>predicate</strong> is commonly understood to be a Boolean-valued function P: X→ {true, false}, called the predicate on X.<br />
Informally, a predicate is a statement that may be true or false depending on the values of its variables. It can be thought of as an operator or function that returns a value that is either true or false. For example, predicates are sometimes used to indicate set membership: when talking about sets, it is sometimes inconvenient or impossible to describe a set by listing all of its elements. Thus, a predicate P(x) will be true or false, depending on whether x belongs to a set. [Ref: WikiPedia (<a href="https://en.wikipedia.org/wiki/Predicate_(mathematical_logic)" target="_blank" rel="noopener">https://en.wikipedia.org/wiki/Predicate_(mathematical_logic)</a>]
<p>In java 8, predicate is an functional interface <em>java.util.function.Predicate&lt;T&gt;</em> which defines an abstract method named test, that accepts an object of generic type T and returns a boolean. Predicate can be used as assignment target for a lambda expression or method reference.</p>
<p><strong>Example:</strong><br />
<pre><code class="java">package com.manishsanger.Predicate;</p>
<p>public class Vehicle {<br />
private String type;<br />
private Integer displacement;</p>
<p>public Vehicle(String type, Integer displacement) {<br />
this.type = type;<br />
this.displacement = displacement;<br />
}</p>
<p>@Override<br />
public String toString() {<br />
return "Vehicle - Type:" + this.getType() + ", Displacement:" + this.getDisplacement() +"cc";<br />
}</p>
<p>public String getType() {<br />
return type;<br />
}</p>
<p>public void setType(String type) {<br />
this.type = type;<br />
}</p>
<p>public Integer getDisplacement() {<br />
return displacement;<br />
}</p>
<p>public void setDisplacement(Integer displacement) {<br />
this.displacement = displacement;<br />
}<br />
}<br />
</code></pre>
<p>If we need to get the vehicle whose displacement is more that 1500 cc, we can write a predicate for it as below:</p>
<pre><code class="java">public class VehiclePredicate {<br />
public static Predicate&lt;Vehicle&gt; isDisplacementAbove1500() {<br />
return vehicle -&gt; vehicle.getDisplacement() &gt;= 1500;<br />
}<br />
}<br />
</code></pre>
<p>We can user this predicate in <em><strong>filter()</strong></em>, to filter list of vehicle by condition in the predicate.</p>
<pre><code class="java">package com.manishsanger.Predicate;</p>
<p>import java.util.Arrays;<br />
import java.util.List;<br />
import java.util.stream.Collectors;</p>
<p>public class Example {</p>
<p>public static void main(String[] args) {<br />
List&lt;Vehicle&gt; vehicles = Arrays.asList(<br />
new Vehicle("SUV", 2200),<br />
new Vehicle("SUV", 1800),<br />
new Vehicle("SEDAN", 1600),<br />
new Vehicle("HATCHBACK", 1400),<br />
new Vehicle("SUV", 2800)<br />
);</p>
<p>//Using default filter() method, using stream and lambda expression.</p>
<p>//return all Vehicles those have displacement more than 1500<br />
System.out.println("All vehicles those have displacement more than 1500, Using default filter() method.");<br />
vehicles.stream().filter(vehicle -&gt; {<br />
return vehicle.getDisplacement() &gt;= 1500;<br />
}).collect(Collectors.toList()).forEach(System.out::println);</p>
<p>//return all Vehicles those have displacement more than 1500<br />
System.out.println("All SUV those have displacement more than 1500, Using default filter() method.");<br />
vehicles.stream().filter(vehicle -&gt; {<br />
return vehicle.getType().equals("SUV") &amp;&amp; vehicle.getDisplacement() &gt;= 1500;<br />
}).collect(Collectors.toList()).forEach(System.out::println);</p>
<p>}<br />
}<br />
</code></pre>
<p>Following will be the output:<br />
<pre><code class="java">All vehicles those have displacement more than 1500, Using default filter() method.<br />
Vehicle - Type:SUV, Displacement:2200cc<br />
Vehicle - Type:SUV, Displacement:1800cc<br />
Vehicle - Type:SEDAN, Displacement:1600cc<br />
Vehicle - Type:SUV, Displacement:2800cc<br />
All SUV those have displacement more than 1500, Using default filter() method.<br />
Vehicle - Type:SUV, Displacement:2200cc<br />
Vehicle - Type:SUV, Displacement:1800cc<br />
Vehicle - Type:SUV, Displacement:2800cc<br />
</code></pre>
<pre><code class="java"></code></pre>
<p>We can also define our own custom filter method(), Now add a <em><strong>filterVehicles()</strong></em> method in <strong>VehiclePredicate</strong> class.<br />
<pre><code class="java">public static List&lt;Vehicle&gt; filterVehicles(List vehicles, Predicate&lt;Vehicle&gt; predicate) {<br />
return vehicles.stream().filter(predicate).collect(Collectors.toList());<br />
}<br />
</code></pre>
<p>Add following code to Main class, to filter data using custom Predicate filter:<br />
<pre><code class="java">//Using custom filter method and java 8 lambda<br />
System.out.println("\nAll vehicles those have displacement more than 1500, Using default custom filter method.");<br />
VehiclePredicate.filterVehicles(vehicles, vehicle -&gt; {<br />
return vehicle.getDisplacement() &gt;= 1500;<br />
}).forEach(System.out::println);<br />
</code></pre>
<p>Here is the output:<br />
<pre><code class="java">All vehicles those have displacement more than 1500, Using default custom filter method.<br />
Vehicle - Type:SUV, Displacement:2200cc<br />
Vehicle - Type:SUV, Displacement:1800cc<br />
Vehicle - Type:SEDAN, Displacement:1600cc<br />
Vehicle - Type:SUV, Displacement:2800cc<br />
</code></pre>
<p>Here is how you can filter the list without lambda expression:<br />
<pre><code class="java">//Using custom filter method and without lambda<br />
System.out.println("\nAll vehicles those have displacement more than 1500, Using default custom filter method without lambda.");<br />
VehiclePredicate.filterVehicles(vehicles, VehiclePredicate.isDisplacementAbove1500()).forEach(System.out::println);<br />
</code></pre>
<p>Here is the output, for the above block of code:<br />
<pre><code class="java">All vehicles those have displacement more than 1500, Using default custom filter method.<br />
Vehicle - Type:SUV, Displacement:2200cc<br />
Vehicle - Type:SUV, Displacement:1800cc<br />
Vehicle - Type:SEDAN, Displacement:1600cc<br />
Vehicle - Type:SUV, Displacement:2800cc<br />
</code></pre>
<p>Java 8 Predicate Methods<br />
Let’s now go through the methods available for Predicate:</p>
<p><strong>1. Default Predicate and (Predicate other)</strong></p>
<p>It returns a composed predicate that represents a logical AND of two predicates.</p>
<p>To understand this let&#8217;s add another predicate in the <strong>VehiclePredicates</strong> class:</p>
<pre><code class="java">public static Predicate&lt;Vehicle&gt; isTypeSUV() {<br />
return vehicle -&gt; vehicle.getType().equals("SUV");<br />
}<br />
</code></pre>
<p>Now we can apply <strong>and()</strong> and <strong>or()</strong> function if we want to predicate for Vehicle of type SUV and displacement more than 1500 cc.</p>
<p>Here is the code for Main method:<br />
<pre><code class="java">//Multiple filter with AND logical operator<br />
System.out.println("\nAll SUV those have displacement more than 1500, Using custom Predicate filter without lambda.");<br />
VehiclePredicate.filterVehicles(vehicles, VehiclePredicate.isDisplacementAbove1500().and(VehiclePredicate.isTypeSUV())).forEach(System.out::println);</p>
<p>//Multiple filter with OR logical operator<br />
System.out.println("\nAll Vehicle those have displacement more than 1500 or SUV, Using custom Predicate filter without lambda.");<br />
VehiclePredicate.filterVehicles(vehicles, VehiclePredicate.isDisplacementAbove1500().or(VehiclePredicate.isTypeSUV())).forEach(System.out::println);<br />
</code></pre>
<p>Here is the output:<br />
<pre><code class="java">All SUV those have displacement more than 1500, Using custom Predicate filter without lambda.<br />
Vehicle - Type:SUV, Displacement:2200cc<br />
Vehicle - Type:SUV, Displacement:1800cc<br />
Vehicle - Type:SUV, Displacement:2800cc</p>
<p>All Vehicle those have displacement more than 1500 or SUV, Using custom Predicate filter without lambda.<br />
Vehicle - Type:SUV, Displacement:2200cc<br />
Vehicle - Type:SUV, Displacement:1800cc<br />
Vehicle - Type:SEDAN, Displacement:1600cc<br />
Vehicle - Type:SUV, Displacement:2800cc<br />
</code></pre>
<p><strong>2. Default Predicate negate()</strong><br />
It return an predicate that represents logical NOT operator.</p>
<p>For example all Vehicles other than SUV:<br />
<pre><code class="java">//default negate(), Logical NOT operator. All vehicles other than SUV<br />
System.out.println("\nAll vehicles other than SUV, using negate()");<br />
VehiclePredicate.filterVehicles(vehicles, VehiclePredicate.isTypeSUV().negate()).forEach(System.out::println);<br />
</code></pre>
<p>Here is the output:<br />
<pre><code class="java">All vehicles other than SUV, using negate()<br />
Vehicle - Type:SEDAN, Displacement:1600cc<br />
Vehicle - Type:HATCHBACK, Displacement:1400cc<br />
</code></pre>
<p><strong>3. Boolean test (T t)</strong><br />
Predicate test evaluates this predicate on the given argument.<br />
For example we can pass an Vehicle object to check if this predicate return true or false:</p>
<p>Add the following code in main method:<br />
<pre><code class="java">//boolean test(T t) predicate<br />
System.out.println("\nboolean test predicate");<br />
Vehicle testVehicle = new Vehicle("SUV", 1000);<br />
System.out.println(VehiclePredicate.isTypeSUV().test(testVehicle));<br />
System.out.println(VehiclePredicate.isDisplacementAbove1500().test(testVehicle));<br />
</code></pre>
<p>Here is the output:<br />
<pre><code class="java">boolean test(T t) predicate<br />
true<br />
false<br />
</code></pre>
<p><strong>4. static Predicate isEqual(Object targetRef)</strong><br />
Returns a predicate that tests if two arguments are equal according to Objects.equals() method.</p>
<p>Let&#8217;s say we overridden <em><strong>equals()</strong></em> method for Vehicle class</p>
<pre><code class="java">@Override<br />
public boolean equals(Object obj) {<br />
Vehicle vehicle = (Vehicle) obj;<br />
if (this.getType().equals(vehicle.getType()) &amp;&amp; this.getDisplacement().equals(vehicle.getDisplacement())) {<br />
return true;<br />
}<br />
return false;<br />
}<br />
</code></pre>
<p>Now let&#8217;s say we have a Vehicle which has standard type &amp; displacement. Then we can get a Predicate, that will test if the given Vehicle is standard or not.</p>
<pre><code class="java">//static Predicate isEqual(Object targetRef)<br />
System.out.println("\nstatic Predicate isEqual(Object targetRef)");<br />
Predicate standardVehiclePredicate = Predicate.isEqual(new Vehicle("SUV", 1800));</p>
<p>System.out.println(standardVehiclePredicate.test(new Vehicle("SUV", 1800)));<br />
System.out.println(standardVehiclePredicate.test(new Vehicle("SUV", 1500)));<br />
</code></pre>
<p>Here is the output:<br />
<pre><code class="java">static Predicate isEqual(Object targetRef)<br />
true<br />
false<br />
</code></pre>
<p><a href="https://github.com/manishsanger/FunctionalInterface/tree/master/src/com/manishsanger/Predicate" target="_blank">Click here to see the source code</a></p>
<p>The post <a href="https://www.manishsanger.com/predicate/">Predicate &#8211; Functional Interface (Java 8)</a> appeared first on <a href="https://www.manishsanger.com">Manish Sanger</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.manishsanger.com/predicate/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Functional Interface &#8211; Java 8</title>
		<link>https://www.manishsanger.com/functional-interface/</link>
					<comments>https://www.manishsanger.com/functional-interface/#comments</comments>
		
		<dc:creator><![CDATA[manishsanger]]></dc:creator>
		<pubDate>Wed, 03 Jan 2018 10:41:05 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[functional interface]]></category>
		<category><![CDATA[predicate]]></category>
		<category><![CDATA[consumer]]></category>
		<category><![CDATA[java 8]]></category>
		<category><![CDATA[lambda]]></category>
		<guid isPermaLink="false">https://www.manishsanger.com/?p=282</guid>

					<description><![CDATA[<p>Java has always been an Object Oriented programming language, it means we can&#8217;t have a function without its class. Other programming languages like C++, PHP, Python, JavaScript and many more, where we write functions and use anywhere, all these languages support functional programming along with Object Oriented programming. Java introduced Functional interface &#38; Lambda Expressions [&#8230;]</p>
<p>The post <a href="https://www.manishsanger.com/functional-interface/">Functional Interface &#8211; Java 8</a> appeared first on <a href="https://www.manishsanger.com">Manish Sanger</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Java has always been an <strong>Object Oriented</strong> programming language, it means we can&#8217;t have a function without its class. Other programming languages like C++, PHP, Python, JavaScript and many more, where we write functions and use anywhere, all these languages support functional programming along with Object Oriented programming.</p>
<p>Java introduced Functional interface &amp; Lambda Expressions in Java 8, to leverage functional programming to reduce verbosity.</p>
<p><strong>What is functional Interface?</strong></p>
<p>A <em>Functional Interface</em> is an interface which specifies exactly one abstract method. Its implementation may be treated as Lambda Expression.</p>
<p>Note: An interface is still a functional interface if it has many default methods, as long as it specifies only one abstract method.</p>
<p>Since default methods have an implementation, they are not abstract. If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface&#8217;s abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere</p>
<p>While the main use of Functional interfaces is for lambda expressions, method references, and constructor references, they can still be used, like any interface, with anonymous classes, implemented by classes, or created by factory methods.</p>
<p>Java 8 provides some inbuilt functional interfaces like <em>Comparable, Runnable, Callable</em> etc.</p>
<p>If you want to define any functional interface of your own, you can use annotation @FunctionalInterface for compiler level errors. It will allow to specify only one abstract method.</p>
<p>Example:</p>
<pre><code class="java">@FunctionalInterface<br />
public interface SomeInterface {<br />
public String someMethod(String param);<br />
}<br />
</code></pre>
<p>A functional interface is valid even if the @FunctionalInterface annotation would be omitted. It is only for informing the compiler to enforce single abstract method inside interface.</p>
<pre><code class="java">Public interface SomeInterface {<br />
String someMethod(String param);<br />
}</p>
<p>//With default method<br />
@FunctionalInterface<br />
Public interface SomeInterface {<br />
public String someMethod(String param);<br />
default void someDefaultMethod(){<br />
//method Implementation<br />
}</p>
<p>}</p>
<p>// When overriding methods from the java.lang.Object</p>
<p>@FunctionalInterface<br />
Public interface SomeInterface {<br />
public String someMethod(String param);<br />
@Override<br />
public String toString();  //Overridden from Object class</p>
<p>@Override<br />
public boolean equals(Object obj); //Overridden from Object class</p>
<p>}<br />
</code></pre>
<p>The signature of abstract method in functional interface is called <strong>Functional descriptor</strong> because it defines the signature of the lambda expression.</p>
<p>Java 8 has introduced several functional interfaces in java.util.function package which can be used to describe the signature of various lambda expressions.</p>
<p>Here is the complete list of functional interfaces available in java.util.functional:</p>
<p>1. <a href="https://www.manishsanger.com/predicate/">Predicate</a><br />
2. Consumer<br />
3. Function&lt;T, R&gt;<br />
4. Supplier<br />
5. UnaryOperator<br />
6. BinaryOperator<br />
7. BiPredicate&lt;L, R&gt;<br />
8. BiConsumer&lt;T, U&gt;<br />
9. BiFunction&lt;T, U, R&gt;</p>
<p>The post <a href="https://www.manishsanger.com/functional-interface/">Functional Interface &#8211; Java 8</a> appeared first on <a href="https://www.manishsanger.com">Manish Sanger</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.manishsanger.com/functional-interface/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>What new features were added in Java8?</title>
		<link>https://www.manishsanger.com/what-new-features-were-added-in-java8/</link>
					<comments>https://www.manishsanger.com/what-new-features-were-added-in-java8/#respond</comments>
		
		<dc:creator><![CDATA[manishsanger]]></dc:creator>
		<pubDate>Tue, 02 Jan 2018 13:31:00 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[java 8]]></category>
		<category><![CDATA[lambda]]></category>
		<category><![CDATA[functional interface]]></category>
		<category><![CDATA[Stream API]]></category>
		<guid isPermaLink="false">https://www.manishsanger.com/?p=272</guid>

					<description><![CDATA[<p>Here are the list of high level features introduced in Java 8 along with lots of enhancement at complier and JVM level: 1. Lambda Expressions Lambda expression is a concise representation of an anonymous function which can be passed as an argument to a method or stored in a variable. Click Here more details 2. [&#8230;]</p>
<p>The post <a href="https://www.manishsanger.com/what-new-features-were-added-in-java8/">What new features were added in Java8?</a> appeared first on <a href="https://www.manishsanger.com">Manish Sanger</a>.</p>
]]></description>
										<content:encoded><![CDATA[<div id="attachment_273" style="width: 681px" class="wp-caption alignnone"><a href="https://www.manishsanger.com/wp-content/uploads/2018/01/Java-8.png"><img fetchpriority="high" decoding="async" aria-describedby="caption-attachment-273" class="size-full wp-image-273" src="https://www.manishsanger.com/wp-content/uploads/2018/01/Java-8.png" alt="New features introduced in java 8" width="671" height="114" srcset="https://www.manishsanger.com/wp-content/uploads/2018/01/Java-8.png 671w, https://www.manishsanger.com/wp-content/uploads/2018/01/Java-8-300x51.png 300w" sizes="(max-width: 671px) 100vw, 671px" /></a><p id="caption-attachment-273" class="wp-caption-text">New features introduced in java 8</p></div>
<p>Here are the list of high level features introduced in Java 8 along with lots of enhancement at complier and JVM level:</p>
<p>1. <a href="https://www.manishsanger.com/lambda-expressions/" target="_blank" rel="noopener" title="Lambda Expressions"><strong>Lambda Expressions</strong></a><br />
Lambda expression is a concise representation of an anonymous function which can be passed as an argument to a method or stored in a variable. <a href="https://www.manishsanger.com/lambda-expressions/" target="_blank" rel="noopener">Click Here more details</a></p>
<p>2. <a href="https://www.manishsanger.com/functional-interface/" target="_blank" rel="noopener" title="Functional Interface"><strong>Functional Interface</strong></a><br />
A Functional Interface is an interface which specifies exactly one abstract method. <a href="https://www.manishsanger.com/functional-interface/" target="_blank" rel="noopener" title="Functional Interface">Click Here more details</a><br />
3. <strong>Method References</strong><br />
4. <strong>Default methods in interface</strong><br />
5. <strong>Optional</strong><br />
6. <strong>Stream API</strong><br />
7. <strong>Date API</strong><br />
8. <strong>Nashorn, JavaScript Engine</strong></p>
<p>The post <a href="https://www.manishsanger.com/what-new-features-were-added-in-java8/">What new features were added in Java8?</a> appeared first on <a href="https://www.manishsanger.com">Manish Sanger</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.manishsanger.com/what-new-features-were-added-in-java8/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<enclosure url="https://www.manishsanger.com/wp-content/uploads/2018/01/Java-8.png" length="17232" type="image/png"/><media:content url="https://www.manishsanger.com/wp-content/uploads/2018/01/Java-8.png" width="671" height="114" medium="image" type="image/png"/>	</item>
	</channel>
</rss>
