{"id":1349,"date":"2012-07-14T11:08:50","date_gmt":"2012-07-14T09:08:50","guid":{"rendered":"http:\/\/doanduyhai.wordpress.com\/?p=1349"},"modified":"2012-07-14T11:08:50","modified_gmt":"2012-07-14T09:08:50","slug":"java-8-lambda-in-details-part-iii-method-and-constructor-referencing","status":"publish","type":"post","link":"https:\/\/www.doanduyhai.com\/blog\/?p=1349","title":{"rendered":"Java 8 Lambda in details part III : Method and constructor referencing"},"content":{"rendered":"<p>In this third post dedicated to Java 8 Lambda expressions, we&#8217;ll look at the new <font size=\"3\"><strong>::<\/strong><\/font> operator introduced to allow method and constructor referencing.<br \/>\n<!--more--><\/p>\n<blockquote><p><strong>DISCLAIMER: all the details exposed in this post were observed related to the JDK 8 demo version as of <font size=\"3\" color=\"red\">July 10th 2012<\/font>. Since the JDK is still in beta, some assertions may not hold in future<\/strong>\n<\/p><\/blockquote>\n<blockquote><p> Please note that all the example code in this post can be found on my GitHub repository <a href=\"https:\/\/github.com\/doanduyhai\/Java8_Lambda_In_Details\" title=\"Java8_Lambda_In_Details\" target=\"_blank\">https:\/\/github.com\/doanduyhai\/Java8_Lambda_In_Details<\/a><\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<h1>I Method referencing<\/h1>\n<p> With the JDK 8 it is possible to create a lambda expression by aliasing an existing method in a class. For this the new <font size=\"3\"><strong>::<\/strong><\/font> operator has been introduced. It is possible to reference a method:<\/p>\n<ol>\n<li>from its <strong>class<\/strong> if it was declared static<\/li>\n<li>from an <strong>instance variable<\/strong> of from the <strong>this<\/strong> keyword for non-static methods<\/li>\n<\/ol>\n<pre class=\"brush: java; highlight: [8,13,18,26,30]; title: ; wrap-lines: false; notranslate\" title=\"\">\npublic interface MethodReferenceSAM\n{\n\tString process(String input);\n}\n\npublic class MethodReference\n{\n\tpublic static String toUpperStatic(String input)\n\t{\n\t\treturn input.toUpperCase();\n\t}\n\n\tpublic String toUpperInstance(String input)\n\t{\n\t\treturn input.toUpperCase();\n\t}\n\n\tpublic String toUpperFromThis(String input)\n\t{\n\t\tMethodReferenceSAM sam = this::toUpperInstance;\n\t\treturn sam.process(input);\n\t}\n\n\tpublic static void main(String... args)\n\t{\n\t\tMethodReferenceSAM sam = MethodReference::toUpperStatic;\n\t\tSystem.out.println(&amp;quot;nntstatic method reference = &amp;quot;+sam.process(&amp;quot;static_input&amp;quot;));\n\t\t\n\t\tMethodReference methodReference = new MethodReference();\n\t\tsam = methodReference::toUpperInstance;\n\t\tSystem.out.println(&amp;quot;nntinstance method reference = &amp;quot;+sam.process(&amp;quot;instance_input&amp;quot;));\n\t\t\n\t\tSystem.out.println(&amp;quot;nnt&amp;quot;this&amp;quot; method reference = &amp;quot;+methodReference.toUpperFromThis(&amp;quot;this_input&amp;quot;));\n\t\tSystem.out.println(&amp;quot;&amp;quot;);\n\t}\n}\n<\/pre>\n<p>The output:<\/p>\n<blockquote style=\"margin-left:15px;\"><p>\nstatic method reference = STATIC_INPUT<\/p>\n<p>instance method reference = INSTANCE_INPUT<\/p>\n<p>&#8220;this&#8221; method reference = THIS_INPUT\n<\/p><\/blockquote>\n<p>Pretty straightforward. With this feature, we can create a method (static or not) and possibly assigned it as lambda expression to two different SAM (<strong>S<\/strong>ingle <strong>A<\/strong>bstract <strong>M<\/strong>ethod) interface, as long as the method signatures match.<\/p>\n<p>Example<\/p>\n<pre class=\"brush: java; highlight: [12,14]; title: ; wrap-lines: false; notranslate\" title=\"\">\npublic interface SAM1\n{\n\tString process(String input);\n}\n\npublic interface SAM2\n{\n\tString differentName(String input);\n}\n\n...\nSAM1 sam1 = MethodReference::toUpperStatic;\n\nSAM1 sam2 = MethodReference::toUpperStatic;\n\n<\/pre>\n<blockquote><p>The above code example can be found on GitHub at <a href=\"https:\/\/github.com\/doanduyhai\/Java8_Lambda_In_Details\" title=\"https:\/\/github.com\/doanduyhai\/Java8_Lambda_In_Details\" target=\"_blank\">https:\/\/github.com\/doanduyhai\/Java8_Lambda_In_Details<\/a>. Just execute the <strong>MethodReference.bat(MethodReference.sh)<\/strong> script<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<h1>II Constructor referencing<\/h1>\n<p>Constructor referencing is quite similar to method referencing. The SAM interface method should have the same signature as the constructor method being used.<\/p>\n<pre class=\"brush: java; highlight: [47,50,53]; title: ; wrap-lines: false; notranslate\" title=\"\">\npublic interface ConstructorReferenceSAM&amp;lt;T&amp;gt;\n{\n\tT whatEverMethodName();\n}\n\npublic interface ConstructorReferenceSAMWithArgs&amp;lt;T, U&amp;gt;\n{\n\tT createMeANewObject(U arg);\n}\n\npublic interface ConstructorReferenceSAMWithParameterizedArg&amp;lt;T, U&amp;gt;\n{\n\tT magic(List&amp;lt;U&amp;gt; arg);\n}\n\npublic class ConstructorReference\n{\n\n\tprivate String content;\n\n\tprivate List&amp;lt;String&amp;gt; contents;\n\n\tpublic ConstructorReference() {\n\t\tthis.content = &amp;quot;created by constructor reference&amp;quot;;\n\t}\n\n\tpublic ConstructorReference(String content) {\n\t\tthis.content = content;\n\t}\n\n\tpublic ConstructorReference(List&amp;lt;String&amp;gt; contents) {\n\t\tthis.contents = contents;\n\t}\n\n\tpublic String getContent()\n\t{\n\t\treturn content;\n\t}\n\n\tpublic List&amp;lt;String&amp;gt; getContents()\n\t{\n\t\treturn contents;\n\t}\n\n\tpublic static void main(String... args)\n\t{\n\t\tConstructorReferenceSAM&amp;lt;ConstructorReference&amp;gt; constructorSam = ConstructorReference::new;\n\t\tSystem.out.println(&amp;quot;nntcontent = &amp;quot;+constructorSam.whatEverMethodName().getContent());\n\t\t\n\t\tConstructorReferenceSAMWithArgs&amp;lt;ConstructorReference,String&amp;gt; constructorSamWithArg = ConstructorReference::new;\t\t\n\t\tSystem.out.println(&amp;quot;nntcontent by arg = &amp;quot;+constructorSamWithArg.createMeANewObject(&amp;quot;created by constructor reference with arg&amp;quot;).getContent());\n\t\t\n\t\tConstructorReferenceSAMWithParameterizedArg&amp;lt;ConstructorReference,String&amp;gt; constructorSamWithParameterizedArg = ConstructorReference::&amp;lt;String&amp;gt;new;\n\t\tSystem.out.println(&amp;quot;nntcontents size by parameterized arg = &amp;quot;+constructorSamWithParameterizedArg.magic(new ArrayList&amp;lt;String&amp;gt;()).getContents().size());\n\t\tSystem.out.println(&amp;quot;&amp;quot;);\n\t\t\n\t}\n}\n<\/pre>\n<p>The output:<\/p>\n<blockquote>\n<p>        content = created by constructor reference<\/p>\n<p>        content by arg = created by constructor reference with arg<\/p>\n<p>        contents size by parameterized arg = 0\n<\/p><\/blockquote>\n<p>For constructor with parameterized arguments, please notice the special syntax for referencing (<strong>line 53<\/strong>) <strong>ConstructorReference::&lt;String&gt;new;<\/strong><\/p>\n<blockquote><p>The above code example can be found on GitHub at <a href=\"https:\/\/github.com\/doanduyhai\/Java8_Lambda_In_Details\" title=\"https:\/\/github.com\/doanduyhai\/Java8_Lambda_In_Details\" target=\"_blank\">https:\/\/github.com\/doanduyhai\/Java8_Lambda_In_Details<\/a>. Just execute the <strong>ConstructorReference.bat(ConstructorReference.sh)<\/strong> script<\/p><\/blockquote>\n<p> To be continued &#8230;<br \/>\n&nbsp;<br \/>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this third post dedicated to Java 8 Lambda expressions, we&#8217;ll look at the new :: operator introduced to allow method and constructor referencing.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6,8],"tags":[],"_links":{"self":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1349"}],"collection":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1349"}],"version-history":[{"count":0,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1349\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1349"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1349"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1349"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}