{"id":1124,"date":"2020-12-04T14:38:51","date_gmt":"2020-12-04T14:38:51","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=1124"},"modified":"2020-12-04T14:40:27","modified_gmt":"2020-12-04T14:40:27","slug":"consumer","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2020\/12\/04\/consumer\/","title":{"rendered":"Consumer <Intf>"},"content":{"rendered":"\n<p class=\"has-drop-cap\">The utility package <code>java.util.function<\/code> has this functional <a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/function\/package-summary.html\">interface Consumer<\/a>. <\/p>\n\n\n\n<p>Each functional interface (Annotated with @<a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/FunctionalInterface.html\">FunctionalInterface<\/a>) conceptually has a single method (abstract) called the functional method to which the lambda expression&#8217;s parameter and return types are matched against. Functional interfaces often represent abstract concepts like functions, actions, or predicates.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Instances of functional interfaces can be created with lambda expressions, method references, or constructor references.<\/p><\/blockquote>\n\n\n\n<figure class=\"wp-block-pullquote has-background has-luminous-vivid-orange-background-color is-style-solid-color\"><blockquote class=\"has-text-color has-white-color\"><p>Functional interface provide target types for lambda expressions and method references.<\/p><cite>Oracle 8 [Java Doc]<\/cite><\/blockquote><\/figure>\n\n\n\n<p>Instance methods<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><th class=\"has-text-align-left\" data-align=\"left\" scope=\"col\">Modifier and Type<\/th><th class=\"has-text-align-right\" data-align=\"right\" scope=\"col\">Method and Description<\/th><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><code>void<\/code><\/td><td class=\"has-text-align-right\" data-align=\"right\"><code><a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/function\/Consumer.html#accept-T-\">accept<\/a>(<a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/function\/Consumer.html\">T<\/a>&nbsp;t)<\/code> Performs this operation on the given argument.<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><code>default <a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/function\/Consumer.html\">Consumer<\/a>&lt;<a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/function\/Consumer.html\">T<\/a>&gt;<\/code><\/td><td class=\"has-text-align-right\" data-align=\"right\"><code><a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/function\/Consumer.html#andThen-java.util.function.Consumer-\">andThen<\/a>(<a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/function\/Consumer.html\">Consumer<\/a>&lt;? super <a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/function\/Consumer.html\">T<\/a>&gt;&nbsp;after)<\/code> Returns a composed <code>Consumer<\/code> that performs, in sequence, this operation followed by the <code>after<\/code> operation.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-pullquote has-background has-luminous-vivid-orange-background-color is-style-solid-color\"><blockquote class=\"has-text-color has-white-color\"><p>Functional interfaces can provide a target type in multiple contexts &#8211; assignment context, method invocation, or cast context<\/p><cite>Oracle 8 [Java Doc]<\/cite><\/blockquote><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Examples<\/h2>\n\n\n\n<p>Let&#8217;s use this <code>Consumer.accept<\/code> method.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Performs this operation on the given argument. <\/p><p><code>accept(T t)<\/code><\/p><\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>package me.samarthya;\n\nimport java.util.function.Consumer;\n\npublic class Main {\n\n    public static void ExampleOne() {\n        Consumer&lt;String> myString = (x) -> System.out.println(x);\n        myString.accept(\"Hello World!\");\n    }\n    public static void main(String&#91;] args) {\n        ExampleOne();\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Main {\n\n    public static void ExampleOne() {\n        Consumer&lt;String> myString = (x) -> System.out.println(x);\n        myString.accept(\"Hello World!\");\n    }\n    public static void ExampleTwo() {\n        Consumer&lt;List&lt;String>> myList = (x) -> x.stream().sorted().forEach( e -> System.out.println(\" Element: \"+ e));\n        List&lt;String> dummyContents = new LinkedList&lt;String>(){\n            {\n                add(\" Hello\");\n                add(\" World\");\n                add(\"!\");\n            }\n        };\n\n        myList.accept(dummyContents);\n    }\n\n    public static void main(String&#91;] args) {\n        System.out.println(\" Ex1.\");\n        ExampleOne();\n        System.out.println(\" Ex2.\");\n        ExampleTwo();\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">andThen(Consumer)<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public static void ExampleThree() {\n        Consumer&lt;List&lt;String>> toUpper = (x) -> x.replaceAll(String::toUpperCase);\n        Consumer&lt;List&lt;String>> toJoin = (x) -> x.stream().map(e -> String.format(\"%s(%d) \", e, e.length())).forEach(e -> System.out.printf(e));\n\n        List&lt;String> dummyContents = new LinkedList&lt;String>() {\n            {\n                add(\"Hello\");\n                add(\"World\");\n                add(\"!\");\n            }\n        };\n\n        toUpper.andThen(toJoin).accept(dummyContents);\n\n    }\n\n    public static void main(String&#91;] args) {\n        System.out.println(\" Ex3.\");\n        ExampleThree();\n    }<\/code><\/pre>\n\n\n\n<p>Outputs<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Ex3.\nHELLO(5) WORLD(5) !(1) <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Helpers<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/function\/Function.html<\/li><li>https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/function\/Consumer.html<\/li><li><a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/function\/Predicate.html\">Interface Predicate&lt;T&gt;<\/a><\/li><\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The utility package java.util.function has this functional interface Consumer. Each functional interface (Annotated with @FunctionalInterface) conceptually has a single method (abstract) called the functional method to which the lambda expression&#8217;s parameter and return types are matched against. Functional interfaces often represent abstract concepts like functions, actions, or predicates. Instances of functional interfaces can be created [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1126,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"image","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[34],"tags":[124,123,43,121],"class_list":["post-1124","post","type-post","status-publish","format-image","has-post-thumbnail","hentry","category-technical","tag-consumer","tag-functional","tag-interface","tag-java","post_format-post-format-image"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/1124","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=1124"}],"version-history":[{"count":0,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/1124\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/1126"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=1124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=1124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=1124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}