JAVA 8 ??? Why JAVA 8 Functional Programming

in #java7 years ago

java 8 and java 9 came in new Era.
Still people are using java 7. we should move to java 8 and java 9 for modularity concept and performance of JVM.

images.png
Java 8 Best Concepts::
(1 )=> Lambda expressions
(a) Lambdas in a nutshell
(b) Where and how to use lambdas
(c)The execute around pattern
(d) Functional interfaces, type inference
(e)Method references
(f) Composing lambdas

Java 7 Code is More Before===>>>>>

Comparator List of Apple byWeight = new Comparator List of Apple() {
public int compare(Apple a1, Apple a2){
return a1.getWeight().compareTo(a2.getWeight());
}
};

lamda.png
in Java 8 Code is Less====>>>
==================

After (with lambda expressions):
Comparator List of Apple byWeight =
(Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight());

Test Lamda Expression in Java 8

package yatra.learn.java.LamdaExpession;

public interface MyNumber {
double getValue();
}

package yatra.learn.java.LamdaExpession;

public class TestLamdaExpression {
public static void main(String[] args) {
//Interface
MyNumber myNumber;

    myNumber = () -> 786.45;
    System.out.println("Constant Value"+myNumber.getValue());

    myNumber = () -> Math.random() * 100;

    System.out.println("Randam value"+myNumber.getValue());
    System.out.println("Another Random Value"+myNumber.getValue());

}

}

Test String Using Lamda Expression java 8 String Reversal::

package yatra.learn.java.LamdaExpession;

public class TestReverseStringlamdaExp {
public static void main(String[] args) {

    StringReverseInterface reverseString = (str) -> {
        String finalReverseString = "";
        for (int i = str.length() - 1; i >= 0; i--)
            finalReverseString += str.charAt(i);
        return finalReverseString;
    };
    System.out.println("String Lamda Reverse String is ::"+reverseString.reverse("Sunil"));
}

}

package yatra.learn.java.LamdaExpession;

public interface StringReverseInterface {
String reverse(String str);
}

Java 8 Lamda expression As Argument Test in java 8 ::

package yatra.learn.java.LamdaExpession;

public class LamdaAsArgumentsTest {
static String stringOp(StringFunc sf , String s) {
return sf.func(s);
}
public static void main(String[] args) {
String inStr = "Sunil Kumar Kanaujia";
String outStr;
System.out.println("Input String::="+inStr);

    outStr = stringOp((str) -> str.toUpperCase(), inStr);
    outStr = stringOp((str) -> {
        String result = "";
        for (int i=0; i<str.length();i++)
            if (str.charAt(i)!= ' ')
                result += str.charAt(i);
        return result;
    },inStr);
    System.out.println("The String With Spaces removed::="+outStr);

    StringFunc reverse = (str) -> {
        String result = "";
        for (int i = str.length()-1; i >= 0; i--)
            result += str.charAt(i);
        return result;
    };
    System.out.println("The String Word Reversed::="+stringOp(reverse,inStr));

}

}

Java 8 Lamda expression Exception Test in Java 8

package yatra.learn.java.LamdaExpession;

public class LamdaExceptionTest {
public static void main(String[] args) throws EmptyArrayException{
double[] values = {1.0,2.0,3.0,4.0};
DoubleNumricArrayFunc average = (n -> {
double sum = 0;

        if(n.length == 0) throw new EmptyArrayException();
        for (int i = 0; i < n.length; i++)
            sum += n[i];
        return sum/n.length;
    });
    System.out.println("The Average is::="+average.func((values)));
    System.out.println("The Average is::="+average.func(new double[0]));
}

}

Java 8 Lamda expression Reference Instance Test in Java 8

package yatra.learn.java.LamdaExpession;
// objectReference :: MethodName gives -> MethodReferenceInstanceTest
public class MethodReferenceInstanceTest {
static String stringOp(StringFunc stringFunc, String s) {
return stringFunc.func(s);
}

public static void main(String[] args) {
    String inStr = "Sunil Kumar Kanaujia";
    String outStr;

    //Create a MyStringOps Object.
    MyStringReverse objectstr = new MyStringReverse();

    //Now a method reference to the instance method strReverse
    //is passed to stringOp()

    outStr = stringOp(objectstr :: stringReverse,inStr);

    System.out.println("Original String::="+inStr);
    System.out.println("Reverse String::="+outStr);
}

}

Java 8 Lamda expression Method reference Test:

package yatra.learn.java.LamdaExpession;

public class MethodReferenceTest {
static String strinfOp(StringFunc stringFunc, String s) {
return stringFunc.func(s);
}
public static void main(String[] args) {
String intStr = "Sunil Kumar Kanaujia";
String outStr;

    //Method reference to strReverse is passed to StringOP().
    outStr = strinfOp(MyStringOps::stringReverse,intStr);

    System.out.println("Original String::="+intStr);
    System.out.println("Reverse String::="+outStr);

}

}

Method Reference With Different Object in Java 8 Lamda expression

stream.jpg

Sort:  

Resteemed your article. This article was resteemed because you are part of the New Steemians project. You can learn more about it here: https://steemit.com/introduceyourself/@gaman/new-steemians-project-launch

Coin Marketplace

STEEM 0.23
TRX 0.22
JST 0.037
BTC 98709.14
ETH 3447.96
USDT 1.00
SBD 3.20