Coder Social home page Coder Social logo

java8's Introduction

Java8

All the Java 8 Related codes

Lamda - It is the implemtation of interface and the only condition is that interface should have only one abstract method

SAM - Single Abstract Method

Java 8 - Functional Interface - keyword

multiple line lamda s- should be written with { };

multiple parameters - more than one parameters can be passed to lambda with ()

Return Values - Lambda can also return values like a function

functional programming - it should not depend on any variable

function as first class objects - assigning a function to a variable

higher order functions - it is the reverse of the normal function where in we create a function to store the data and we call that function with different actions

ex : consider a scenario of string uppercase, lowercase and then length

general approach is

public String toUppercase(String input) { return input.toUpperCase(); }

public String toLowercase(String input) { return input.toLowerCase(); }

public String lengthFinder(String input) { return input.length(); }

and then

we write a main method to invoke

main() { toLowerCase("LambDA"); toUpperCase("LambDA"); lengthFinder("LambDA"); }

whereas in lambda

consider the interface called welcome

public interface welcome { String greet(String name); }

in the main method

main () { test(n) -> n.toUpperCase(); test(x) -> x.toLowerCase(); test(h) -> h.length(); }

public static void test(welcome w) { System.out.println(w.greet("lambda"); }

Method References - shorter way of writing the clean code for the lambda

public interface methodref {

String change(String input); }

main() { //methodref mr = n -> n.toUpperCase();

methodref mr = String::toUpperCase; System.out.println(mr.change("lambda"));

below are some of the ways //System.out::println //String::concat - with two parameters

}

Method reference can even add to the list

consider the below example

public interface lamda_list { void add(String input); }

main () { List list = new ArrayList<>();

lamda_list ll = x -> list.add(x); -- without method reference

lamda_list ll = list::add; - with method reference

ll.add("Anyname"); }

Description Lambda Expression Lambda using method reference
Static Method call - pass as parameter-
method accepts data and prints using System.out.println
(data) -> System.out.println(data) System.out::println
Static Method call - pass as parameter-
method accepts data and we pass it to another method to check if it is null
(o) -> Objects.isNull(o) Objects::isNull
Given Object - Instance method call-
for example, call the toUpperCase for the given string
(data) -> data.toUpperCase() String::toUpperCase
Given Object - Instance method call with parameter -
for example, call the concat for the given string
(s1,s2) -> s1.contact(s2) String::Concat
Given Object - Instance method call with parameters-
for example, call the replaceAll for the given string with given parameters
(s1,s2,s3) -> s1.replaceAll(s2,s3) String::replaceAll
Given Object - Pass as parameter-
pass the given object to another object method as parameter
(data) -> list.add(data) list::add
Create New Object - New Cat() () -> new cat() cat::new

The above table reference is taken from Vinoth Selvaraj blog (https://www.vinsguru.com/vinoth-selvaraj/)

Functional Interfaces

  1. Supplier - The Supplier Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which does not take in any argument but produces a value of type T.

The Supplier interface consists of only one function:

  1. get() Supplier randomValue = () -> Math.random(); Supplier randomValue = Math::random;

     // Print the random value using get()
     System.out.println(randomValue.get());
    

it is useful in implemeting the driver factory in test automation

sample code below

private static final Supplier<WebDriver> chromeSupplier = () -> {
    System.setProperty("webdriver.chrome.driver","D:\\Technology\\Tools\\drivers\\chrome\\chromedriver.exe");
    return new ChromeDriver();
};

private  static final Supplier<WebDriver> firefoxsupplier = () -> {
    System.setProperty("webdriver.gecko.driver","D:\\Technology\\Tools\\drivers\\firefox\\geckodriver.exe");
    return new FirefoxDriver();
};

private static Map<String,Supplier<WebDriver>> MAP = new HashMap<>();

static {
    MAP.put("chrome",chromeSupplier);
    MAP.put("firefox",firefoxsupplier);
}

public static WebDriver getDriver(String Browser) {
    return MAP.get(Browser).get();
}
  1. The Consumer Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in one argument and produces a result. However these kind of functions don’t return any value. Hence this functional interface which takes in one generic namely:-

T: denotes the type of the input argument to the operation The lambda expression assigned to an object of Consumer type is used to define its accept() which eventually applies the given operation on its argument. Consumers are useful when it not needed to return any value as they are expected to operate via side-effects.

Functions in Consumer Interface The Consumer interface consists of the following two functions:

  1. accept()

Consumer c = (s) -> System.out::println; c.accept("sample");

Functional Interface

image taken from https://www.waytoeasylearn.com/learn/supplier-functional-interface/

java8's People

Contributors

shivasubramaniamr avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.