{"id":156,"date":"2022-08-08T18:35:58","date_gmt":"2022-08-08T18:35:58","guid":{"rendered":"https:\/\/valerio.nu\/?p=156"},"modified":"2022-08-08T18:37:41","modified_gmt":"2022-08-08T18:37:41","slug":"java-8-predicate","status":"publish","type":"post","link":"https:\/\/valerio.nu\/index.php\/2022\/08\/08\/java-8-predicate\/","title":{"rendered":"Java 8 Predicate"},"content":{"rendered":"\n<p>The functional interface Predicate was defined in Java 8. Predicate receives an argument and based on a specific condition it returns a Boolean value. It makes the code more readable, manageable and clean. It\u2019s located in the java.util.function package.<\/p>\n\n\n\n<p>Predicate benefits mainly unit-testing; it has several built-in methods that lead to better tests results.<\/p>\n\n\n\n<p>Let&#8217;s look at the following example: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static void main(String&#91;] args) {\n\n    Predicate&lt;Integer&gt; predicate = n -&gt; (n &lt; 20);\n    System.out.println(predicate.test(10));\n    \n}\n<\/code><\/pre>\n\n\n\n<p>Here, we send to the Predicate an integer value of 10. Then, we provide the logic that the argument number which is represented by <em>n<\/em> should be less than 20 for the predicate to be true, otherwise it returns false. In the above example, the predicate is obviously true and the output is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>true<\/code><\/pre>\n\n\n\n<p>A few more examples: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static void main(String&#91;] args) {\n\n    Predicate&lt;Integer&gt; predicate1 = n -&gt; (n &gt; 20);\n    System.out.println(predicate1.test(10));\n\n    Predicate&lt;Integer&gt; predicate2 = n -&gt; (n == 10);\n    System.out.println(predicate2.test(5));\n\n    Predicate&lt;Integer&gt; predicate3 = n -&gt; (n &lt; 10);\n    System.out.println(predicate3.test(2));\n\n}\n<\/code><\/pre>\n\n\n\n<p>With outputs:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> false\n false\n true\n<\/code><\/pre>\n\n\n\n<p>In the above examples, we are using the Predicate through the method test(). In addition to that, Predicate has four more methods: isEqual() , and() , or() , negate() :<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>test(): returns true if the argument matches the condition, otherwise false.<\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li>isEqual: returns true if two arguments are equal according to object\u2019s equals() method, otherwise false.<\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li>and(): it is used when there are multiple logical conditions that are checked by the Predicate, it returns true only if all conditions are true, otherwise false<\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li>or(): it is used when there are multiple logical conditions that are checked by the Predicate, it returns true if one of the conditions is true. If all the conditions are false then it returns false.<\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li>negate(): it is the opposite of the test() method. It returns false if the argument matches the condition and returns true if the argument doesn\u2019t match the condition<\/li><\/ul>\n\n\n\n<p>We saw an example for test(), let\u2019s show a few examples for the other methods:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>isEqual():<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>public static void main(String&#91;] args) {\n\n    Predicate&lt;String&gt; predic = Predicate.isEqual(\"Hello World\");\n\n    System.out.println(predic.test(\"Hello World\"));\n    System.out.println(predic.test(\"Hello\"));\n\n\n}\n<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>true\nfalse\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>and():<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>public static void main(String&#91;] args) {\n\n    Predicate&lt;Integer&gt; predicate1 = n -&gt; (n &gt; 20);\n\n\n    Predicate&lt;Integer&gt; predicate2 = n -&gt; (n == 25);\n\n    Predicate&lt;Integer&gt; andPredicate = predicate1.and(predicate2);\n    System.out.println(andPredicate.test(30));\n    System.out.println(andPredicate.test(25));\n\n}\n<\/code><\/pre>\n\n\n\n<p>Output: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> false\n true\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>or():<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>public static void main(String&#91;] args) {\n\n    Predicate&lt;Integer&gt; predicate1 = n -&gt; (n &gt; 20);\n\n\n    Predicate&lt;Integer&gt; predicate2 = n -&gt; (n == 25);\n\n    Predicate&lt;Integer&gt; andPredicate = predicate1.or(predicate2);\n    System.out.println(andPredicate.test(30));\n    System.out.println(andPredicate.test(25));\n    System.out.println(andPredicate.test(15));\n\n}\n<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>true\ntrue\nfalse\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>negate():<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>public static void main(String&#91;] args) {\n\n    Predicate&lt;Integer&gt; predicate1 = i -&gt; i &gt; 15;\n\n    Predicate&lt;Integer&gt; negate = predicate1.negate();\n\n    System.out.println(negate.test(30));\n    System.out.println(negate.test(10));\n\n}\n<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>false\ntrue\n<\/code><\/pre>\n\n\n\n<p>One of the main uses of Predicate is to apply a Filter to a collection containing objects.<br>What does Filter in Java Stream do?<\/p>\n\n\n\n<p>Filter is an interface used to filter out some of the collection\u2019s objects based on a predefined condition. Let\u2019s see an example of Filter:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static void main(String&#91;] args) {\n\n    List&lt;Integer&gt; ages = Arrays.asList(23, 16, 26, 31, 44, 12, 8, 20, 41, 33, 17);\n\n    List&lt;Integer&gt; collect = ages.stream().filter(x -&gt; x &gt; 18).collect(Collectors.toList());\n\n    System.out.println(\"The ages above 18 : \" + collect);\n\n}\n<\/code><\/pre>\n\n\n\n<p>Above we have a list with different ages and we want to pick only the ages above 18.<\/p>\n\n\n\n<p>The output: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The ages above 18 : &#91;23, 26, 31, 44, 20, 41, 33]\n<\/code><\/pre>\n\n\n\n<p>Now let\u2019s see how Filter works in combination with Predicate:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static void main(String&#91;] args) {\n\n    Predicate&lt;Integer&gt; above18 = x -&gt; x &gt; 18;\n\n    List&lt;Integer&gt; ages = Arrays.asList(23, 16, 26, 31, 44, 12, 8, 20, 41, 33, 17);\n\n    List&lt;Integer&gt; collect = ages.stream().filter(above18).collect(Collectors.toList());\n\n    System.out.println(\"The ages above 18 : \" + collect);\n\n}\n<\/code><\/pre>\n\n\n\n<p>In this example, we give to the filter() method an argument of Predicate type. The output is as following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The ages above 18 : &#91;23, 26, 31, 44, 20, 41, 33]<\/code><\/pre>\n\n\n\n<p>In this article we saw what is Predicate, its benefits and its usages. Then we saw how to use it in combination with other stream interfaces to produce an easier and cleaner Java code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The functional interface Predicate was defined in Java 8. Predicate receives an argument and based on a specific condition it returns a Boolean value. It [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-156","post","type-post","status-publish","format-standard","hentry","category-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java 8 Predicate - Andy&#039;s blog \u2935\ufe0f<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/valerio.nu\/index.php\/2022\/08\/08\/java-8-predicate\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java 8 Predicate - Andy&#039;s blog \u2935\ufe0f\" \/>\n<meta property=\"og:description\" content=\"The functional interface Predicate was defined in Java 8. Predicate receives an argument and based on a specific condition it returns a Boolean value. It [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/valerio.nu\/index.php\/2022\/08\/08\/java-8-predicate\/\" \/>\n<meta property=\"og:site_name\" content=\"Andy&#039;s blog \u2935\ufe0f\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-08T18:35:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-08-08T18:37:41+00:00\" \/>\n<meta name=\"author\" content=\"andy\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"andy\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2022\\\/08\\\/08\\\/java-8-predicate\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2022\\\/08\\\/08\\\/java-8-predicate\\\/\"},\"author\":{\"name\":\"andy\",\"@id\":\"https:\\\/\\\/valerio.nu\\\/#\\\/schema\\\/person\\\/da69cc3da309893b0d3bc8fcef75f128\"},\"headline\":\"Java 8 Predicate\",\"datePublished\":\"2022-08-08T18:35:58+00:00\",\"dateModified\":\"2022-08-08T18:37:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2022\\\/08\\\/08\\\/java-8-predicate\\\/\"},\"wordCount\":425,\"commentCount\":0,\"articleSection\":[\"Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2022\\\/08\\\/08\\\/java-8-predicate\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2022\\\/08\\\/08\\\/java-8-predicate\\\/\",\"url\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2022\\\/08\\\/08\\\/java-8-predicate\\\/\",\"name\":\"Java 8 Predicate - Andy&#039;s blog \u2935\ufe0f\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/valerio.nu\\\/#website\"},\"datePublished\":\"2022-08-08T18:35:58+00:00\",\"dateModified\":\"2022-08-08T18:37:41+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/valerio.nu\\\/#\\\/schema\\\/person\\\/da69cc3da309893b0d3bc8fcef75f128\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2022\\\/08\\\/08\\\/java-8-predicate\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2022\\\/08\\\/08\\\/java-8-predicate\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2022\\\/08\\\/08\\\/java-8-predicate\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/valerio.nu\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java 8 Predicate\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/valerio.nu\\\/#website\",\"url\":\"https:\\\/\\\/valerio.nu\\\/\",\"name\":\"Andy&#039;s blog \u2935\ufe0f\",\"description\":\"Abandon all hope, ye who enter here\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/valerio.nu\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/valerio.nu\\\/#\\\/schema\\\/person\\\/da69cc3da309893b0d3bc8fcef75f128\",\"name\":\"andy\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4852a025c6250c852814d4017f646677730a23c1ec14eb5ca36b69dcce5135a5?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4852a025c6250c852814d4017f646677730a23c1ec14eb5ca36b69dcce5135a5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4852a025c6250c852814d4017f646677730a23c1ec14eb5ca36b69dcce5135a5?s=96&d=mm&r=g\",\"caption\":\"andy\"},\"sameAs\":[\"https:\\\/\\\/valerio.nu\"],\"url\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/author\\\/giuseppe\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java 8 Predicate - Andy&#039;s blog \u2935\ufe0f","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:\/\/valerio.nu\/index.php\/2022\/08\/08\/java-8-predicate\/","og_locale":"en_US","og_type":"article","og_title":"Java 8 Predicate - Andy&#039;s blog \u2935\ufe0f","og_description":"The functional interface Predicate was defined in Java 8. Predicate receives an argument and based on a specific condition it returns a Boolean value. It [&hellip;]","og_url":"https:\/\/valerio.nu\/index.php\/2022\/08\/08\/java-8-predicate\/","og_site_name":"Andy&#039;s blog \u2935\ufe0f","article_published_time":"2022-08-08T18:35:58+00:00","article_modified_time":"2022-08-08T18:37:41+00:00","author":"andy","twitter_card":"summary_large_image","twitter_misc":{"Written by":"andy","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/valerio.nu\/index.php\/2022\/08\/08\/java-8-predicate\/#article","isPartOf":{"@id":"https:\/\/valerio.nu\/index.php\/2022\/08\/08\/java-8-predicate\/"},"author":{"name":"andy","@id":"https:\/\/valerio.nu\/#\/schema\/person\/da69cc3da309893b0d3bc8fcef75f128"},"headline":"Java 8 Predicate","datePublished":"2022-08-08T18:35:58+00:00","dateModified":"2022-08-08T18:37:41+00:00","mainEntityOfPage":{"@id":"https:\/\/valerio.nu\/index.php\/2022\/08\/08\/java-8-predicate\/"},"wordCount":425,"commentCount":0,"articleSection":["Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/valerio.nu\/index.php\/2022\/08\/08\/java-8-predicate\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/valerio.nu\/index.php\/2022\/08\/08\/java-8-predicate\/","url":"https:\/\/valerio.nu\/index.php\/2022\/08\/08\/java-8-predicate\/","name":"Java 8 Predicate - Andy&#039;s blog \u2935\ufe0f","isPartOf":{"@id":"https:\/\/valerio.nu\/#website"},"datePublished":"2022-08-08T18:35:58+00:00","dateModified":"2022-08-08T18:37:41+00:00","author":{"@id":"https:\/\/valerio.nu\/#\/schema\/person\/da69cc3da309893b0d3bc8fcef75f128"},"breadcrumb":{"@id":"https:\/\/valerio.nu\/index.php\/2022\/08\/08\/java-8-predicate\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/valerio.nu\/index.php\/2022\/08\/08\/java-8-predicate\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/valerio.nu\/index.php\/2022\/08\/08\/java-8-predicate\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/valerio.nu\/"},{"@type":"ListItem","position":2,"name":"Java 8 Predicate"}]},{"@type":"WebSite","@id":"https:\/\/valerio.nu\/#website","url":"https:\/\/valerio.nu\/","name":"Andy&#039;s blog \u2935\ufe0f","description":"Abandon all hope, ye who enter here","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/valerio.nu\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/valerio.nu\/#\/schema\/person\/da69cc3da309893b0d3bc8fcef75f128","name":"andy","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4852a025c6250c852814d4017f646677730a23c1ec14eb5ca36b69dcce5135a5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4852a025c6250c852814d4017f646677730a23c1ec14eb5ca36b69dcce5135a5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4852a025c6250c852814d4017f646677730a23c1ec14eb5ca36b69dcce5135a5?s=96&d=mm&r=g","caption":"andy"},"sameAs":["https:\/\/valerio.nu"],"url":"https:\/\/valerio.nu\/index.php\/author\/giuseppe\/"}]}},"_links":{"self":[{"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/posts\/156","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/comments?post=156"}],"version-history":[{"count":6,"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/posts\/156\/revisions"}],"predecessor-version":[{"id":172,"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/posts\/156\/revisions\/172"}],"wp:attachment":[{"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/media?parent=156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/categories?post=156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/tags?post=156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}