{"id":487,"date":"2015-10-25T20:48:42","date_gmt":"2015-10-25T20:48:42","guid":{"rendered":"http:\/\/mlapshin.com\/?p=487"},"modified":"2015-10-26T13:22:35","modified_gmt":"2015-10-26T13:22:35","slug":"handy-lambdas","status":"publish","type":"post","link":"https:\/\/mlapshin.com\/index.php\/2015\/10\/25\/handy-lambdas\/","title":{"rendered":"Handy Lambdas"},"content":{"rendered":"<p>Hi guys!<\/p>\n<p>Do you still not use <a href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/java\/javaOO\/lambdaexpressions.html\" target=\"_blank\">Lambdas<\/a> in Java? Today I will show you how event-based projects\u00a0can\u00a0benefit from them.<\/p>\n<h2>Nice small\u00a0listeners<\/h2>\n<p>Lambdas could\u00a0be considered as a handy replacement for anonymous classes. Vaadin is an event-based framework. So, before Java 8 our PIM project\u00a0had tons of code like this:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nbutton.addClickListener(new Button.ClickListener() {\r\n \r\n private static final long serialVersionUID = -8692988480268369082L;\r\n\r\n public void buttonClick(ClickEvent event) {\r\n     layout.addComponent(new Label(&quot;Thank you for clicking&quot;));\r\n }\r\n});\r\n<\/pre>\n<p>Note, how much clearer\u00a0the code is\u00a0when it\u00a0uses a Lambda expression instead of the anonymous class:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nbutton.addClickListener(event -&gt; layout.addComponent(new Label(&quot;Thank you for clicking&quot;))); \r\n<\/pre>\n<p>The following unnecessary code was removed:<\/p>\n<ul>\n<li><strong><code>new<\/code> <\/strong>keyword together with the listener interface name<\/li>\n<li><strong><code>serialVersionUID<\/code> <\/strong>field<\/li>\n<li>the single interface method declaration<\/li>\n<\/ul>\n<p>Even if a\u00a0listener method has several lines, it also looks good:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nbutton.addClickListener(event -&gt; {\r\n    Label label = new Label(&quot;Thank you for clicking&quot;);\r\n    layout.addComponent(label);\r\n});\r\n<\/pre>\n<p>Another benefit of Lambdas is direct access to parent class fields without adding parent class name before <strong><code>this<\/code> <\/strong>keyword like:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n    this.field\r\n<\/pre>\n<p>Instead of<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n    MyClass.this.field\r\n<\/pre>\n<h2>Eclipse trick<\/h2>\n<p>Eclipse has a useful shortcut to automatically convert similar anonymous classes\u00a0into Lambda expressions. For this purpose do the following:<br \/>\n1. Delete the <strong><code>serialVersionUID<\/code> <\/strong>line. Note, the listener interface name is underlined as a warning.<br \/>\n2. Place the keyboard\u00a0cursor on the\u00a0listener interface name and press <strong>Ctrl+1<\/strong><br \/>\n3. In the appeared popup menu select <strong><code>Convert to lambda expression<\/code><\/strong><br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-500 alignnone\" src=\"https:\/\/mlapshin.com\/wp-content\/uploads\/2015\/10\/eclipse_trick.jpg\" alt=\"Lambda Eclipse Trick\" width=\"628\" height=\"140\" srcset=\"https:\/\/mlapshin.com\/wp-content\/uploads\/2015\/10\/eclipse_trick.jpg 628w, https:\/\/mlapshin.com\/wp-content\/uploads\/2015\/10\/eclipse_trick-300x67.jpg 300w\" sizes=\"auto, (max-width: 628px) 100vw, 628px\" \/><\/p>\n<h2>Lambdas in\u00a0Stream API<\/h2>\n<p>Another useful application of Lambda expressions is\u00a0<a href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/collections\/streams\/index.html\" target=\"_blank\">Stream API<\/a>. It is a kind of replacement for iterators. Streams\u00a0are especially useful if you need to do many sequential manipulations on Collection items or process big Collections effectively.<\/p>\n<p>Below is a simplified example from the PIM\u00a0project code that represents a list of Entity objects as a String. The code does the following steps:<\/p>\n<ol>\n<li>Retains only entities from the Master\u00a0catalog<\/li>\n<li>Sorts entities by ID<\/li>\n<li>Forms String representation for\u00a0each\u00a0Entity object<\/li>\n<li>Concatenates all Entity String representations into one String like 1@1,3@1,7@1<\/li>\n<\/ol>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nString result = entityList.stream() \r\n    .filter(entity -&gt; MASTER.equals(entity.getCatalogId()))\r\n    .sorted(Comparator.comparing(Entity::getId))\r\n    .map(entity -&gt; entity.getId() + AT + entity.getCatalogId())\r\n    .collect( Collectors.joining( COMMA ) );\r\n<\/pre>\n<p>Think how much more code it would require to do the same with usual <strong><code>for<\/code> <\/strong>operator.<\/p>\n<p>I hope I persuaded you to learn a bit more about Lambda expressions and start using them in your applications.<\/p>\n<p>bye!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi guys! Do you still not use Lambdas in Java? Today I will show you how event-based projects\u00a0can\u00a0benefit from them. Nice small\u00a0listeners Lambdas could\u00a0be considered as a handy replacement for anonymous classes. Vaadin is an event-based framework. So, before Java&hellip; <br \/><a class=\"read-more-button\" href=\"https:\/\/mlapshin.com\/index.php\/2015\/10\/25\/handy-lambdas\/\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":512,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,3],"tags":[6,19,25],"class_list":["post-487","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","category-programming","tag-how-to","tag-java","tag-lambda"],"_links":{"self":[{"href":"https:\/\/mlapshin.com\/index.php\/wp-json\/wp\/v2\/posts\/487","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mlapshin.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mlapshin.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mlapshin.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mlapshin.com\/index.php\/wp-json\/wp\/v2\/comments?post=487"}],"version-history":[{"count":40,"href":"https:\/\/mlapshin.com\/index.php\/wp-json\/wp\/v2\/posts\/487\/revisions"}],"predecessor-version":[{"id":530,"href":"https:\/\/mlapshin.com\/index.php\/wp-json\/wp\/v2\/posts\/487\/revisions\/530"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mlapshin.com\/index.php\/wp-json\/wp\/v2\/media\/512"}],"wp:attachment":[{"href":"https:\/\/mlapshin.com\/index.php\/wp-json\/wp\/v2\/media?parent=487"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mlapshin.com\/index.php\/wp-json\/wp\/v2\/categories?post=487"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mlapshin.com\/index.php\/wp-json\/wp\/v2\/tags?post=487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}