Friday, August 2, 2024

Java 8

 What is map-reduce in streams?

  • Map-Reduce is a functional programming model which serves our 2 purpose.
    • Map- Transform Data
    • Reduce- Aggregating data
  • It combines element in a stream and produces a single value.
  • Example stream of integers and to calculate sum of those integers.
  • Map Transform Stream<Object> to Stream Int.
  • Reduce - Combine Stream of Integer and produce the sum result.
  • T reduce(T identity,BinaryOperator<T> accumulator);
    • Identity is initial value of type T.
    • Accumulator is the function for combining two values.
  • Integer someResult  = Stream.of(2,4,6).reduce(0,(a,b)->a+b);
  • Reduce is used for Aggregation.

Can we override default method in an interface?

  • Yes, we can override default method in an interface
  • When we are implementing two interfaces with same method, and we want a particular interface to be used, we override The default method in interface to select, which one of the two interfaces will be used.
  • The syntax is Interface. Super. Method().

What are the benefits of using streams?

  • A stream is a pipeline Of aggregate operations that process a sequence of elements.
  • Behaviour parameters exclamation simplify coping with changing requirements.
  • A stream is conceptually , unbounded, though it is often bound by practical constraints.
  • They are concise and readable.
  • Focus on what function should perform instead of how to perform them.
  • Flexible that is functions or automatically compared together output of one becomes input of another
    • Thus, we can perform multiple operations at once.
  • Stable that is parallel performance without The need to write any multi-threaded code.
  • Parallel streams run from a pool of threads And the pool used is common fork join pool.
    • Common fork join pool is a singleton.
    • It works on task, stealing
  • We don’t have to worry about thread management.
  • Threads created are called as worker threads.
    • Thread safe code is there in parallel streams
  • Data is automatically mapped to processor cores.
  • In a non-stream, the flow is sequential Which is not a good approach and stream provides para processing making it faster.

What is lazy evaluation in streams?

What is the difference between static methods and default methods in an interface?

  • Static methods in interfaces is similar to default methods, just that it’s static. Hence its implementation cannot be changed in child classes.
  • Implementing class need not and cannot change the definition
  • Static methods of interface can’t be overridden.If they are overridden, then That process is called as method hiding.
  • These are called through interface Name that is interface.static method name.
  • Interface, static method helps us in providing security by not allowing implementation classes to override them.
  • Interface, static methods are good for providing utility methods, for example, null checking, I.e Who’s definition will never change.

Why Java 8 was introduced?

  • Significant reason four introducing Java eight was to introduce conciseness in code.
  • Java brings in functional programming which is enabled by Lambda expressions.
  • With Python,Scala we can do the same thing in very less LOC. Java upgraded it’s self from only object oriented programming structure language to some concepts of functional programming to create concise code base.
Q What are the new features that got introduced in Java eight?
  • Lambda expressions
  • Streams API
    • This was an enhancement in collection framework
      • We can have Intermediate methods
      • We can have terminal methods
  • Default methods in the interface
  • Static methods in the interface
  • Functional interface
    • This introduced functional programming.
    • As a part of functional programming we get lambda, we get method references.
  • Optional
  • Method references
  • Date API
    • This is a dedicated package for time
  • Nashhorn, JavaScript engine
Q What are the main advantages of using Java eight?
  • Compact code
  • More readable and reusable code
  • More testable code
  • Parallel operations
Q What is lambda expression?
  • Lambda expression is and anonymous function without having (name, return type and access modifier) and having one lambda symbol (->).
  • Example
Normal Programming Equivalent Lambda Expression
public void add(int a,int b) {
    System.out.println(a+b);
}
BiConsumer<Integer,Integer> biConsumer = (a,b) -> System.out.println(a+b);
  • Remove name, return type, access modifier and types of arguments from the function to get lambda function.
  • In the above example a,b has been inferred from a functional interface such as Bi Consumer which takes in arguments and give output has nothing.
    •      BiConsumer<Integer,Integer> biConsumer = (a,b) -> System.out.println(a+b);
    •      biConsumer.accept(5, 10);
Q What are functional interfaces?
  • Functional interfaces all those interfaces which have only one abstract method.
  • It can have any number of static method, default methods. No restriction on that.
  • There are many Functional interfaces already present in Java such as example comparable, runnable.
  • Functional interfaces are used as a reference to lambda expressions.
Q How can we create our own functional interface?
  • As we know functional interface Is an interface with exactly one single Abstract method and can have multiple static or default methods.
  • To create our own functional interface
    • Create an interface
    • Annotate that with @Functional Interface
    • Define exactly one abstract method.
    • There is no restriction on number of static and default methods defined in such an interface.
    • Java can implicitly identify functional interface but still you can also annotate it with @functional Interface.
      • It just gives you security that in case if you by mistake add 2 abstract methods then compiler will give a compile time error.
Q What is method reference in Java 8?
  • Method reference is replacement of lambda expressions.
  • It is used to refer method of functional interface to an existing method.
  • Mainly it is used for code reusability.
  • Functional interfaces abstract method can be mapped to specific existing method using double colon operator(::)
  • This is method reference.
  • Hence method reference is an alternative to Lambda expression.
  • Whenever we have existing implementation of abstract method of our functional interface then we can go for method reference.
  • Example of method reference 
  •  @FunctionalInterface  
     public interface FunctionalInterfaceDemo {  
     void singleAbstractMethod();  
     }  
     public class Test {  
     public static void testImplementation() {  
          System.out.println("This is an distinct Method");  
     }  
     }  
     public class MethodReferenceDemo {  
          public static void main(String[] args) {  
               FunctionalInterfaceDemo functionalInterfaceDemo= Test::testImplementation;  
               functionalInterfaceDemo.singleAbstractMethod();  
          }  
     }  
    
  • So we see in class method reference demo we can replace lambda expression with method references.
  • If we do not have a method Reference then only we can use is Lambda expressions which is called as Single Abstract Method.
Q What is difference between method reference and Single Abstract Method?
  • In a single abstract method we only have a functional interface with abstract method. We can only use lambda expressions.
  • In a Method reference We also have a class with another abstract method which defines its implementation.
Q What is the advantage of using functional expression in lambda?
  • Enable functional programming
  • Readable and concise code
  • Easier to use API and libraries
  • And enable support for parallel processing.
  • We can pass behaviour as the argument to the object.

No comments:

Post a Comment