{"id":1791,"date":"2021-04-10T10:15:18","date_gmt":"2021-04-10T10:15:18","guid":{"rendered":"https:\/\/gauravw.com\/blog\/?p=1791"},"modified":"2021-04-10T10:15:22","modified_gmt":"2021-04-10T10:15:22","slug":"java-8","status":"publish","type":"post","link":"https:\/\/gauravw.com\/blog\/2021\/04\/java-8\/","title":{"rendered":"Java 8"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Lambda Expressions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">They are nameless functions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>To enable functional programming<\/li><li>Write concise and readable code<\/li><li>Use API effectively<\/li><li>Parallel programming<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">How to write them<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>No return type<\/li><li>No modifier<\/li><li>No name<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Public String lengthOfString(String s) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">return s.length;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">(s) -&gt; {s.length;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">s -&gt; s.length;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Functional Interface<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">An Interface that contains just one abstract method. It can contain any number of default and static methods. They are present in the java.util package.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Examples<br>Runnable, Callable, Comparable<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can declare an interface FI by @FunctionalInterface. This Is not necessary but it helps for reporting compile errors in case you use more than 1 abstract method or none of them.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If another FI extends the interface then it needs to ensure only one method is present in the interface which will come from the parent.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Usage<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Comparator Interface to sort List &#8211; for descending order<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Collections.sort(myList, new MyComparator());<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Collections.sort(myList, ( i, j ) &#8211; &gt; ( ( i &gt; j ) ? -1 : ( i &lt; j ) ? 1 : 0 );<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\"><li>TreeSet\/TreeMap sorting using Lambda Expression<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">TreeMap&lt;Integer, String&gt; tm = new TreeMap&lt;&gt;( new myComparator() );<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">TreeMap&lt;Integer, String&gt; tm = new TreeMap&lt;&gt;( ( i, j ) &#8211; &gt; ( ( i &gt; j ) ? -1 : ( i &lt; j ) ? 1 : 0 );<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">TreeMap sorts according to keys.<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\"><li>Custom objects sorting<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Assume list of Employee objects<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Collections.sort (<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;listOfEmployee, (e1, e2) -&gt; ( (e1.id&gt;e2.id) ? 1: (e1.uid&lt;e2.id) ? -1 : 0 ) )<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Anonymous Inner class Class &amp; Lambda Expressions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Anonymous Inner class class<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Runnable r = new Runnable () {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">public void run(){<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u2026.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">};<br><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Lambda Expressions<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Runnable r = () -&gt; {};<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Differences<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Anonymous Inner class can be made for concrete class, abstract class, interfaces. <strong>\u03bb <\/strong>only for interface (with just one method).<\/li><li>Anonymous Inner class will have<strong> .class<\/strong> generated for it and not for <strong>\u03bb<\/strong><\/li><li>\u201cthis\u201d in Anonymous Inner class points to instance variables(variables in the class are wrapped). \u201cthis\u201d in \u03bb points to local variables.&nbsp;<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Class A {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Int c=1;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Runnable r = () -&gt; {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;c=2;<br>S.O.P(c); \/\/ prints 2<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">};<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\"><li>Memory for AIC is cleared in heap and for <strong>\u03bb <\/strong>in the method area.<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">AIC are more powerful than <strong>\u03bb<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/ZLeBWMIHr2MXQOrVG0vrnllXp2RruIM8FCim6sFuVe1AC-yCHxxuq1Gm6fDgDJHcAfwBs88Oc91xKE7mGU8kIwe8bEIAAEZQ8VKuPy1X1GbblPf99UW4D-e80rP3TUnKGGRkV8Pp\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Closure<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here <strong>\u03bb <\/strong>expression can access class variables. It can also access local variables of the function they are declared in. However, if they access local variables then local variables become final.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So you can&#8217;t change their value.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is a closure&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, if you don&#8217;t use local method level variables then you can change the variable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Default Methods in Interfaces<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Interfaces can have default methods.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If there is multiple inheritance in the class from two different interfaces with the same default method then there could be ambiguity.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">class A implements iA, iB {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">public void common () {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">iA.super.common();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So we can resolve ambiguity by the above method of specifying which interface\u2019s method to be used.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, if two interfaces with similar abstract methods are there then it will work.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But if two interfaces with similar abstract methods and conflicting return types in an error.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Interface with default methods != Abstract classes<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>every variable in the interface will be public static final.&nbsp;<\/li><li>No instance variables.<\/li><li>No constructors&nbsp;<\/li><li>Abstract class cant refer lambda expressions<\/li><li>We can&#8217;t override object methods in interfaces<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Static methods in Interfaces<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Static methods are now allowed.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, they are never inherited in the implementation classes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So we can have the same named function in those classes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also one can now have the main method in interfaces as it is also a static method. We can run the main method of the Interface.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Predicate Functional Interface<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">It is a functional interface used for boolean testing. It has only one abstract method \u201c<strong>test<\/strong>\u201d and it can take only one parameter.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Predicate&lt;Integer&gt; result = a -&gt; (a &gt; 10);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">result.test(100); \/\/ true<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Similar to an interface like this<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">BooleanInterface result = new BooleanInterface {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">public boolean test(Integer a) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If (a &gt; 10)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">return true;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">return false;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There can be predicate joining.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>negate<\/strong>() &#8211; p1.negate();<\/li><li><strong>or<\/strong>() &#8211; p1.or(p2);<\/li><li><strong>and<\/strong>() &#8211; p1.and(p2);<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">So two conditions are tested based on these joins. These are default methods in Predicate Interface.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Predicate has a static method \u201c<strong>isEqual<\/strong>()\u201d<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Predicate&lt;Integer&gt; p = Predicate.isEqual(10);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">p.test(10); \/\/true<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Function Functional Interface<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">It is a functional interface in which we can have return type as anything. Predicate is a special type of function interface where return type is boolean. It has only one abstract method \u201c<strong>apply<\/strong>\u201d and it can take only one parameter.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Function&lt;T,R&gt; name;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example &#8211; count spaces in string<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Function&lt;String,Integer&gt; spaceCount = s -&gt; ( s.length &#8211; s.replaceAll(\u201c \u201d).length);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">spaceCount.apply(\u201cHi what&#8217;s going on\u201d);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Additionally, you can change the values of the object using this as it acts as a function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Function Chaining(default methods)<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>andThen <\/strong>&#8211; f1.andThen(f2).apply(\u201c\u201d) &#8211; first f1 will be called then f2<\/li><li><strong>compose <\/strong>&#8211; f1.compose(f2).apply(\u201c\u201d) &#8211; first f2 will be called then f1<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Static method &#8211; f1.identity().apply(\u201c\u201d) &#8211; just returns the same thing as input parameter<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Consumer Functional Interface<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">It just \u201cconsumes\u201d.It is similar to Function FI just that return type is void.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Void public accept(T t)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Consumer&lt;Student&gt; display&nbsp; =&nbsp; s-&gt;{ System.out.println( \u201cName\u201d +&nbsp; s.getName() );<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">System.out.println(\u201cGrade\u201d + s.getGrade() )&nbsp; };<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Consumer Chaining<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>andThen &#8211; c1.andThen(c2).accept(t);<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Supplier Functional Interface<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">It just \u201csupplies\u201d. It is similar to Function FI just that it has no argument but it has a return type.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">public R get();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Supplier&lt;Integer&gt; currentDate = () -&gt; ( return new Date(); )<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">currentDate.get();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No chaining is possible here.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">BiPredicate Functional Interface<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Similar to predicate but takes 2 arguments. It has the same chaining concept.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">BiPredicate&lt;Integer,Integer&gt; isEven = (a,b)-&gt;((a+b)%2==0);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">isEven.test(2,3);\/\/false<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">BiFunction Functional Interface<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Similar to Function but takes 2 arguments. It has the same chaining concept.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">BiFunction&lt;Integer,String,Student&gt; productString = (a,b)-&gt;new Student(a,b);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">productString .apply(2,\u201dgaurav\u201d);\/\/ return Student object<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">BiConsumer Functional Interface<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Similar to Consumer but takes 2 arguments. It has the same chaining concept.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">BiConsumer&lt;Integer,String&gt; display= (a,b)-&gt;{S.O.P(\u201croll no=\u201d + a + \u201c, name = \u201d + name)};<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">display.get(2,\u201dgaurav\u201d);\/\/roll no=2, name=gaurav<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Primitive Type Functional Interface<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Due to autoboxing and autounboxing, there will be a performance hit for the above interfaces in case we have input and output as primitive types.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is because there will always be conversion.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So we have primitive type interfaces IntPredicate, LongPredicate etc<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">IntFunction FI &#8211; accepts int<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">ToIntFunction FI- return int<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">IntToDouble FI- takes int and returns double<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">IntConsumer &#8211; accepts int<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">IntObjConsumer &#8211; accepts int and object type<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">IntSupplier &#8211; returns int<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If input and output is of the same type use UnaryOperator FI instead of Function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If all inputs and output are of the same type use BinaryOperator FI instead of Function<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Double Colon Operator :: &#8211; Method Reference Operator<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This can be used instead of a lambda expression for giving a reference of another function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&lt;Class name&gt;<strong>::<\/strong>&lt;method name&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Class Test{<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Public void m1(){<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Some lines here<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">PSVM(){<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Test mytest = new Test();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Runnable r = () -&gt; {};<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Runnable r = <strong>mytest::m1<\/strong>;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Static Method<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">ClassName::methodName<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\"><li>Non Static Method<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">instanceOfClass::methodName<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">System.out::println<br>Lambda Alternative (s-&gt;System.out.println())<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\"><li>Constructor<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">MyClass::new<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\"><li>Super Method<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">super::someSuperClassMethod<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Streams<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To process a collection of objects we use streams.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Stream s = c.stream();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Stream &#8211; is an interface in java.util.stream package.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">C &#8211; is any collection.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">stream() &#8211; is a default function inside every collection.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Filter &#8211; selects elements based on the predicate<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">public Stream filter(Predicate&lt;T&gt; p)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Stream s = c.stream().filter(i -&gt; (i%2==0) );<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\"><li>Map &#8211; map the value of every object in the collection to a new value\/create a new object<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">public Stream map(Function&lt;T,R&gt; f)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Stream s = c.stream().map(i -&gt; i * 2);<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\"><li>Collect &#8211; collects elements from the stream and adds to a collection<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">List&lt;Integer&gt; s = c.stream().map(i -&gt; i * 2).collect( Collectors.toList() );<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">List&lt;String&gt; s = c.stream().map(s-&gt; s.toUppercase() ).collect( Collectors.toList() );<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\"><li>Count &#8211; Counts the elements in the stream<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Integer evenNoCount = c.stream().filter(i -&gt; (i%2==0)).count();<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"5\"><li>Sorted &#8211; Sorts a collection<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">List&lt;Integer&gt; sortedNo = noList.stream().sorted().collect( Collectors.toList());<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">sorted(Comparator comparator ) &#8211; sorted( (i,j)-&gt; ( i.compareTo(j) ));<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"6\"><li>Min and Max<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Integer min= noList.stream().min((i,j) -&gt; i.compareTo(j)).get();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Integer max= noList.stream().max((i,j) -&gt; i.compareTo(j)).get();<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"7\"><li>forEach<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">noList.stream().forEach(System.out::println);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">noList.stream().forEach(s- &gt; S.O.P(s));<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"8\"><li>toArray<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Integer ar[] = noList.stream().toArray(Integer[]::new);<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"9\"><li>Stream.of &#8211; can be applied to an array<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Stream s = Stream.of(\u201chi\u201d, \u201cmy\u201d, \u201cname\u201d, \u201cgaurav\u201d) ;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Date Time Api&nbsp;<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The date\/time API, before Java 8, presented multiple design problems.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Among the problems is the fact that the <em>Date<\/em> and <em>SimpleDateFormatter<\/em> classes aren&#8217;t thread-safe. To address this issue, <strong>Joda-Time uses immutable classes for handling date and time.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <em>Date<\/em> class doesn&#8217;t represent an actual date, but instead, it specifies an instant in time, with millisecond precision. The year in a <em>Date<\/em> starts from 1900, while most of the date operations usually use Epoch time which starts from January 1st, 1970.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, the day, month and year offset of a <em>Date<\/em> is counterintuitive. Days start at 0, while month begins at 1. To access any of them, we must use the <em>Calendar<\/em> class. <strong>Joda-Time offers a clean and fluent API for handling dates and time.<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>LocalDate<\/li><li>LocalTime<\/li><li>LocalDAteTime<\/li><li>ZoneId<\/li><li>Period<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Map And Flat Map<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Stream.flatMap, as it can be guessed by its name, is the combination of a map and a flat operation. That means that you first apply a function to your elements, and then flatten it. Stream.map only applies a function to the stream without flattening the stream.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To understand what flattening a stream consists in, consider a structure like [ [1,2,3],[4,5,6],[7,8,9] ] which has &#8220;two levels&#8221;. Flattening this means transforming it in a &#8220;one level&#8221; structure : [ 1,2,3,4,5,6,7,8,9 ].<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Map &#8211; One to One<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">List&lt;String> myList = Stream.of(&#8220;a&#8221;, &#8220;b&#8221;)\u00a0\u00a0.<strong>map<\/strong>(String::toUpperCase)\u00a0.collect(Collectors.toList()); \/\/ [a , b]<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">FlatMap &#8211; One to Many &#8211; Map + Flat<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">List&lt;List&lt;String&gt;&gt; list = Arrays.asList(<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;Arrays.asList(&#8220;a&#8221;),<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;Arrays.asList(&#8220;b&#8221;));<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">System.out.println(list\u00a0\u00a0.stream()\u00a0\u00a0.<strong>flatMap<\/strong>(Collection::stream)) \/\/[a. b]<\/p>\n\n\n","protected":false},"excerpt":{"rendered":"<p>Lambda Expressions They are nameless functions. Use To enable functional programming Write concise and readable code Use API effectively Parallel programming How to write them No return type No modifier No name Public String lengthOfString(String s) { return s.length; } (s) -&gt; {s.length;} s -&gt; s.length; Functional Interface An Interface that contains just one abstract [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[97],"tags":[],"class_list":["post-1791","post","type-post","status-publish","format-standard","hentry","category-tech-learnings"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java 8 &#187; Gaurav Wadhwani<\/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:\/\/gauravw.com\/blog\/2021\/04\/java-8\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java 8 &#187; Gaurav Wadhwani\" \/>\n<meta property=\"og:description\" content=\"Lambda Expressions They are nameless functions. Use To enable functional programming Write concise and readable code Use API effectively Parallel programming How to write them No return type No modifier No name Public String lengthOfString(String s) { return s.length; } (s) -&gt; {s.length;} s -&gt; s.length; Functional Interface An Interface that contains just one abstract [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gauravw.com\/blog\/2021\/04\/java-8\/\" \/>\n<meta property=\"og:site_name\" content=\"Gaurav Wadhwani\" \/>\n<meta property=\"article:published_time\" content=\"2021-04-10T10:15:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-04-10T10:15:22+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/lh5.googleusercontent.com\/ZLeBWMIHr2MXQOrVG0vrnllXp2RruIM8FCim6sFuVe1AC-yCHxxuq1Gm6fDgDJHcAfwBs88Oc91xKE7mGU8kIwe8bEIAAEZQ8VKuPy1X1GbblPf99UW4D-e80rP3TUnKGGRkV8Pp\" \/>\n<meta name=\"author\" content=\"Gaurav Wadhwani\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Gaurav Wadhwani\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/java-8\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/java-8\\\/\"},\"author\":{\"name\":\"Gaurav Wadhwani\",\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/#\\\/schema\\\/person\\\/9a05a9c3487f35f6b4577c6956cf252e\"},\"headline\":\"Java 8\",\"datePublished\":\"2021-04-10T10:15:18+00:00\",\"dateModified\":\"2021-04-10T10:15:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/java-8\\\/\"},\"wordCount\":1758,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/#\\\/schema\\\/person\\\/9a05a9c3487f35f6b4577c6956cf252e\"},\"image\":{\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/java-8\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/lh5.googleusercontent.com\\\/ZLeBWMIHr2MXQOrVG0vrnllXp2RruIM8FCim6sFuVe1AC-yCHxxuq1Gm6fDgDJHcAfwBs88Oc91xKE7mGU8kIwe8bEIAAEZQ8VKuPy1X1GbblPf99UW4D-e80rP3TUnKGGRkV8Pp\",\"articleSection\":[\"Tech Learnings\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/java-8\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/java-8\\\/\",\"url\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/java-8\\\/\",\"name\":\"Java 8 &#187; Gaurav Wadhwani\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/java-8\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/java-8\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/lh5.googleusercontent.com\\\/ZLeBWMIHr2MXQOrVG0vrnllXp2RruIM8FCim6sFuVe1AC-yCHxxuq1Gm6fDgDJHcAfwBs88Oc91xKE7mGU8kIwe8bEIAAEZQ8VKuPy1X1GbblPf99UW4D-e80rP3TUnKGGRkV8Pp\",\"datePublished\":\"2021-04-10T10:15:18+00:00\",\"dateModified\":\"2021-04-10T10:15:22+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/java-8\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/java-8\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/java-8\\\/#primaryimage\",\"url\":\"https:\\\/\\\/lh5.googleusercontent.com\\\/ZLeBWMIHr2MXQOrVG0vrnllXp2RruIM8FCim6sFuVe1AC-yCHxxuq1Gm6fDgDJHcAfwBs88Oc91xKE7mGU8kIwe8bEIAAEZQ8VKuPy1X1GbblPf99UW4D-e80rP3TUnKGGRkV8Pp\",\"contentUrl\":\"https:\\\/\\\/lh5.googleusercontent.com\\\/ZLeBWMIHr2MXQOrVG0vrnllXp2RruIM8FCim6sFuVe1AC-yCHxxuq1Gm6fDgDJHcAfwBs88Oc91xKE7mGU8kIwe8bEIAAEZQ8VKuPy1X1GbblPf99UW4D-e80rP3TUnKGGRkV8Pp\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/2021\\\/04\\\/java-8\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java 8\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/\",\"name\":\"Gaurav Wadhwani\",\"description\":\"Where I write \\\/ scribble\",\"publisher\":{\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/#\\\/schema\\\/person\\\/9a05a9c3487f35f6b4577c6956cf252e\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/gauravw.com\\\/blog\\\/#\\\/schema\\\/person\\\/9a05a9c3487f35f6b4577c6956cf252e\",\"name\":\"Gaurav Wadhwani\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/788ed9666a6c4e011516ae9c744df4be274dcf933161c99a4ec7e06311d2d416?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/788ed9666a6c4e011516ae9c744df4be274dcf933161c99a4ec7e06311d2d416?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/788ed9666a6c4e011516ae9c744df4be274dcf933161c99a4ec7e06311d2d416?s=96&d=mm&r=g\",\"caption\":\"Gaurav Wadhwani\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/788ed9666a6c4e011516ae9c744df4be274dcf933161c99a4ec7e06311d2d416?s=96&d=mm&r=g\"},\"sameAs\":[\"http:\\\/\\\/gauravw.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java 8 &#187; Gaurav Wadhwani","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:\/\/gauravw.com\/blog\/2021\/04\/java-8\/","og_locale":"en_US","og_type":"article","og_title":"Java 8 &#187; Gaurav Wadhwani","og_description":"Lambda Expressions They are nameless functions. Use To enable functional programming Write concise and readable code Use API effectively Parallel programming How to write them No return type No modifier No name Public String lengthOfString(String s) { return s.length; } (s) -&gt; {s.length;} s -&gt; s.length; Functional Interface An Interface that contains just one abstract [&hellip;]","og_url":"https:\/\/gauravw.com\/blog\/2021\/04\/java-8\/","og_site_name":"Gaurav Wadhwani","article_published_time":"2021-04-10T10:15:18+00:00","article_modified_time":"2021-04-10T10:15:22+00:00","og_image":[{"url":"https:\/\/lh5.googleusercontent.com\/ZLeBWMIHr2MXQOrVG0vrnllXp2RruIM8FCim6sFuVe1AC-yCHxxuq1Gm6fDgDJHcAfwBs88Oc91xKE7mGU8kIwe8bEIAAEZQ8VKuPy1X1GbblPf99UW4D-e80rP3TUnKGGRkV8Pp","type":"","width":"","height":""}],"author":"Gaurav Wadhwani","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Gaurav Wadhwani","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/gauravw.com\/blog\/2021\/04\/java-8\/#article","isPartOf":{"@id":"https:\/\/gauravw.com\/blog\/2021\/04\/java-8\/"},"author":{"name":"Gaurav Wadhwani","@id":"https:\/\/gauravw.com\/blog\/#\/schema\/person\/9a05a9c3487f35f6b4577c6956cf252e"},"headline":"Java 8","datePublished":"2021-04-10T10:15:18+00:00","dateModified":"2021-04-10T10:15:22+00:00","mainEntityOfPage":{"@id":"https:\/\/gauravw.com\/blog\/2021\/04\/java-8\/"},"wordCount":1758,"commentCount":0,"publisher":{"@id":"https:\/\/gauravw.com\/blog\/#\/schema\/person\/9a05a9c3487f35f6b4577c6956cf252e"},"image":{"@id":"https:\/\/gauravw.com\/blog\/2021\/04\/java-8\/#primaryimage"},"thumbnailUrl":"https:\/\/lh5.googleusercontent.com\/ZLeBWMIHr2MXQOrVG0vrnllXp2RruIM8FCim6sFuVe1AC-yCHxxuq1Gm6fDgDJHcAfwBs88Oc91xKE7mGU8kIwe8bEIAAEZQ8VKuPy1X1GbblPf99UW4D-e80rP3TUnKGGRkV8Pp","articleSection":["Tech Learnings"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/gauravw.com\/blog\/2021\/04\/java-8\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/gauravw.com\/blog\/2021\/04\/java-8\/","url":"https:\/\/gauravw.com\/blog\/2021\/04\/java-8\/","name":"Java 8 &#187; Gaurav Wadhwani","isPartOf":{"@id":"https:\/\/gauravw.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/gauravw.com\/blog\/2021\/04\/java-8\/#primaryimage"},"image":{"@id":"https:\/\/gauravw.com\/blog\/2021\/04\/java-8\/#primaryimage"},"thumbnailUrl":"https:\/\/lh5.googleusercontent.com\/ZLeBWMIHr2MXQOrVG0vrnllXp2RruIM8FCim6sFuVe1AC-yCHxxuq1Gm6fDgDJHcAfwBs88Oc91xKE7mGU8kIwe8bEIAAEZQ8VKuPy1X1GbblPf99UW4D-e80rP3TUnKGGRkV8Pp","datePublished":"2021-04-10T10:15:18+00:00","dateModified":"2021-04-10T10:15:22+00:00","breadcrumb":{"@id":"https:\/\/gauravw.com\/blog\/2021\/04\/java-8\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gauravw.com\/blog\/2021\/04\/java-8\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/gauravw.com\/blog\/2021\/04\/java-8\/#primaryimage","url":"https:\/\/lh5.googleusercontent.com\/ZLeBWMIHr2MXQOrVG0vrnllXp2RruIM8FCim6sFuVe1AC-yCHxxuq1Gm6fDgDJHcAfwBs88Oc91xKE7mGU8kIwe8bEIAAEZQ8VKuPy1X1GbblPf99UW4D-e80rP3TUnKGGRkV8Pp","contentUrl":"https:\/\/lh5.googleusercontent.com\/ZLeBWMIHr2MXQOrVG0vrnllXp2RruIM8FCim6sFuVe1AC-yCHxxuq1Gm6fDgDJHcAfwBs88Oc91xKE7mGU8kIwe8bEIAAEZQ8VKuPy1X1GbblPf99UW4D-e80rP3TUnKGGRkV8Pp"},{"@type":"BreadcrumbList","@id":"https:\/\/gauravw.com\/blog\/2021\/04\/java-8\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gauravw.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Java 8"}]},{"@type":"WebSite","@id":"https:\/\/gauravw.com\/blog\/#website","url":"https:\/\/gauravw.com\/blog\/","name":"Gaurav Wadhwani","description":"Where I write \/ scribble","publisher":{"@id":"https:\/\/gauravw.com\/blog\/#\/schema\/person\/9a05a9c3487f35f6b4577c6956cf252e"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gauravw.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/gauravw.com\/blog\/#\/schema\/person\/9a05a9c3487f35f6b4577c6956cf252e","name":"Gaurav Wadhwani","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/788ed9666a6c4e011516ae9c744df4be274dcf933161c99a4ec7e06311d2d416?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/788ed9666a6c4e011516ae9c744df4be274dcf933161c99a4ec7e06311d2d416?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/788ed9666a6c4e011516ae9c744df4be274dcf933161c99a4ec7e06311d2d416?s=96&d=mm&r=g","caption":"Gaurav Wadhwani"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/788ed9666a6c4e011516ae9c744df4be274dcf933161c99a4ec7e06311d2d416?s=96&d=mm&r=g"},"sameAs":["http:\/\/gauravw.com"]}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/posts\/1791","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/comments?post=1791"}],"version-history":[{"count":1,"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/posts\/1791\/revisions"}],"predecessor-version":[{"id":1792,"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/posts\/1791\/revisions\/1792"}],"wp:attachment":[{"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/media?parent=1791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/categories?post=1791"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gauravw.com\/blog\/wp-json\/wp\/v2\/tags?post=1791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}