Why Java8

 Java is the most popular and widely accepted language in the world. 

 Recognizing the need for more efficient and modern programming capabilities, Java creators introduced functional features such as 

  • Lambdas, 
  • Streams,
  • Optionals. 

These enhancements, along with the technology advancements in mobile, laptop, and system processors with multiple cores, have significantly simplified concurrency operations.

Functional Programming :

Functional programming embraces the creation of immutable objects, resulting in more concise and readable code.

It also promotes using functions and methods as first-class citizens.

Example :



        🠆Write Declarative programming approach rather than imperative approach.

Imperative Style of programming :

    🠆Focus on how to perform operations.

    🠆Embrace the object mutability

   🠆This style of programming list the step by step of instructions how to achieve an objective.

   🠆We write the code on each step what needs to be done.

  🠆Imperative style used with classic object oriented programming.

    Example 1: Finding sum 

ImperativeVsDeclarativeExample1
// sum of integers for the range from 0 to 100
/**
* Imperative Style - how style of programming
*/ int sum=0; for(int i=0;i<=100;i++){ sum+=i; // shared mutable state and its sequential anf it will go step by step // and it will have issues if we try to run the code in multithreaded environment } System.out.println("Sum is : "+sum);




Declarative Programming :

    🠆Focus on what results you want.

    🠆Embrace the immutability

    🠆Analogous to SQL

    🠆Use the functions that are already part of the library to achieve an objective

    🠆Functional programming uses the concepts of Declarative programming.

    

import java.util.function.Predicate; import java.util.stream.IntStream;
/**
* Declarative style. (Functional programming uses the same style)
* what style of programming.
* You let the system do the job for you and get the result.
*/
int sum1= IntStream.rangeClosed(0,100)
//.parallel() //multi threading env
.map(Integer::new)
.sum();
System.out.println("sum1 : " + sum1);

    Example 2 : Removing the duplicates from the list of integers.

Imperative style

List<Integer> integerList =Arrays.asList(1,2,3,4,4,5,5,6,7,7,8,9,9); //Remove the duplicates from the list. /** * Imperative Style */ List<Integer> uniqueList = new ArrayList<>();         for(Integer i :integerList)             if(!uniqueList.contains(i)){             uniqueList.add(i); } System.out.println("unique List : " + uniqueList);

  Declarative Style

import static java.util.stream.Collectors.toList; /*** Declarative Syle */ List<Integer> uniqueList1 = integerList.stream()                                         .distinct()                                         .collect(Collectors.toList());    System.out.println("uniqueList1 : " + uniqueList1);



What is Lamda Expression :

            Lamda is equivalent to a function without a name

            Lamdas are also referred as as Anonymous function 

            it has all the qualities that the regular method has such as

                    Method params

                    methods body

                    return type

        Lamdas not tied to any class like a regular method

       Lamda can also be assigned to any other variable and passed around.

 

    Syntax :   ( ) 🠆 { }

Usages :

        Main purpose is to implement functional interfaces-  SAM(Single abstract method) 

        @FunctionalInterface

           interface comparator<T> {

int compare(T o1, T o2);

        }

            

@FunctionalInterface

interface Runnable {

public abstract run();

}

            

Comments