{"id":4793,"date":"2023-11-30T15:54:28","date_gmt":"2023-11-30T15:54:28","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=4793"},"modified":"2023-11-30T16:10:53","modified_gmt":"2023-11-30T16:10:53","slug":"java-8-new-features","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/java-8-new-features\/","title":{"rendered":"Java 8 New Features"},"content":{"rendered":"<ol>\n<li><strong class=\"ctaHeader3\">forEach method in java.lang.Iterable interface<\/strong>\n<p>Java 8 has introduced forEach method in java.lang.Iterable interface so that while writing code we focus on business logic. The forEach method takes java.util.function.Consumer object as an argument, so it helps in having our business logic at a separate location that we can reuse.<\/li>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nmyList.forEach(new Consumer &lt; Integer &gt; () {\r\n    public void accept(Integer t) {\r\n        System.out.println(&quot;forEach anonymous class Value::&quot; + t);\r\n    }\r\n\r\n});\r\n\r\n\/\/traversing with Consumer interface implementation\r\nMyConsumer action = new MyConsumer();\r\nmyList.forEach(action);\r\n<\/pre>\n<p>Consumer implementation that can be reused<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nclass MyConsumer implements Consumer&lt;Integer&gt;{\r\n\r\n\tpublic void accept(Integer t) {\r\n\t\tSystem.out.println(&quot;Consumer impl Value::&quot;+t);\r\n\t}\r\n}\r\n<\/pre>\n<li><strong class=\"ctaHeader3\">Interfaces with  default and static methods<\/strong>\n<p>From Java 8, interfaces are enhanced to have a method with implementation. We can use default and static keyword to create interfaces with method implementation<\/li>\n<p>We know that Java doesn\u2019t provide multiple inheritance in Classes because it leads to Diamond Problem. So how it will be handled with interfaces now since interfaces are now similar to abstract classes. Compiler will throw an exception in this scenario and we will have to provide implementation logic in the class implementing the interfaces.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@FunctionalInterface\r\npublic interface Interface1 {\r\n\r\n    void method1(String str);\r\n\r\n    default void log(String str) {\r\n        System.out.println(&quot;I1 logging::&quot; + str);\r\n    }\r\n\r\n    static void print(String str) {\r\n        System.out.println(&quot;Printing &quot; + str);\r\n    }\r\n}    \r\n<\/pre>\n<p><strong>interfaces are not allowed to have Object default methods.<\/strong><\/p>\n<li><strong class=\"ctaHeader3\"> @FunctionalInterface annotation.<\/strong> Functional interfaces are a new concept introduced in Java 8. An interface with exactly one abstract method becomes a Functional Interface. We don\u2019t need to use @FunctionalInterface annotation to mark an interface as a Functional Interface.\n<p>@FunctionalInterface annotation is a facility to avoid the accidental addition of abstract methods in the functional interfaces. You can think of it like @Override annotation and it\u2019s best practice to use it. java.lang.Runnable with a single abstract method run() is a great example of a functional interface.<\/p>\n<p><strong>Anonymous Class Implementation<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nRunnable r = new Runnable() {\r\n    @Override\r\n    public void run() {\r\n        System.out.println(&quot;My Runnable&quot;);\r\n    }\r\n};\r\n<\/pre>\n<p><strong>Lambda Expression Implementation<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nRunnable r1 = () - &gt; {\r\n    System.out.println(&quot;My Runnable&quot;);\r\n};\r\n<\/pre>\n<p>If you have single statement in method implementation anonymous class can be instantiated using lambda expression as below<br \/>\n<strong>lambda expressions Implementation<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nInterface1 i1 = (s) -&gt; System.out.println(s);\t\t\r\ni1.method1(&quot;abc&quot;);\r\n<\/pre>\n<\/li>\n<li>New <strong class=\"ctaHeader3\">java.util.stream <\/strong>has been added in Java 8 to perform filter\/map\/reduce like operations with the collection. Stream API will allow sequential as well as parallel execution.\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic static void main(String&#x5B;] args) {\r\n\r\n    List &lt; Integer &gt; myList = new ArrayList &lt; &gt; ();\r\n    for (int i = 0; i &lt; 100; i++) myList.add(i);\r\n\r\n    \/\/sequential stream\r\n    Stream &lt; Integer &gt; sequentialStream = myList.stream();\r\n\r\n    \/\/parallel stream\r\n    Stream &lt; Integer &gt; parallelStream = myList.parallelStream();\r\n\r\n    \/\/using lambda with Stream API, filter example\r\n    Stream &lt; Integer &gt; highNums = parallelStream.filter(p - &gt; p &gt; 90);\r\n    \/\/using lambda in forEach\r\n    highNums.forEach(p - &gt; System.out.println(&quot;High Nums parallel=&quot; + p));\r\n\r\n    Stream &lt; Integer &gt; highNumsSeq = sequentialStream.filter(p - &gt; p &gt; 90);\r\n    highNumsSeq.forEach(p - &gt; System.out.println(&quot;High Nums sequential=&quot; + p));\r\n\r\n}\r\n<\/pre>\n<p><em>parallel processing values are not in order, so parallel processing will be very helpful while working with huge collections.<\/em>\n <\/li>\n<li><strong class=\"ctaHeader3\">IO improvements<\/strong> known to me are:\n<p>Files.list(Path dir) that returns a lazily populated Stream, the elements of which are the entries in the directory.<br \/>\nFiles.lines(Path path) that reads all lines from a file as a Stream.<br \/>\nFiles.find() that returns a Stream that is lazily populated with Path by searching for files in a file tree rooted at a given starting file.<br \/>\nBufferedReader.lines() that return a Stream, the elements of which are lines read from this BufferedReader.<\/li>\n<li> <strong>Java Time API packages<\/strong>, I can sense that they will be very easy to use. It has some sub-packages java.time.format that provides classes to print and parse dates and times and java.time.zone provides support for time zones and their rules.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>forEach method in java.lang.Iterable interface Java 8 has introduced forEach method in java.lang.Iterable interface so that while writing code we focus on business logic. The forEach method takes java.util.function.Consumer object as an argument, so it helps in having our business logic at a separate location that we can reuse. myList.forEach(new Consumer &lt; Integer &gt; ()&hellip; <a href=\"https:\/\/codethataint.com\/blog\/java-8-new-features\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[260,249],"tags":[],"class_list":["post-4793","post","type-post","status-publish","format-standard","hentry","category-interview-questions-java-8","category-java-8"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4793","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=4793"}],"version-history":[{"count":5,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4793\/revisions"}],"predecessor-version":[{"id":4799,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4793\/revisions\/4799"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=4793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=4793"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=4793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}