Predicate

 

Predicate
A predicate is a function with a single argument and returns Boolean value.
To implement predicate functions in java, oracle people introduced Predicate
interface in 1.8 version
Predicate interface present in java.util.function package
It’s a functional interface and it contains only one method i.e., test ()

Ex:
Interface Predicate<T>{
    Public Boolean test(T t);
}

Example 1:
Write a predicate to check whether the given integer is greater than 10 or not.
Program:
public Boolean test(Integer I)
{
If (I>10){
return true;
}
else {
return false;
}
}


   

(Integer I) -> { if(I>10)
            return true;
            else
            return false
}

   

I->(I>10)

   

Predicate<Integer> p = I->(I->10);
System.out.println(p.test(1000)); //true
System.out.println(p.test(6)); //false

Difference between Interface and Abstract Class.

 

 

Interface

Abstract Class

1

If we don’t know about the implementation, just we have requirement specification then we should go for Interface

If we are talking about implementation but not completely (partial implementation) then we should go for Abstract class.

2

Inside interface every method is always public and abstract whether we are declaring or not. Hence interface is also considered as 100% pure Abstract class.

Every method present in Abstract class need not be public and Abstract. In addition to abstract methods we can take concrete methods also.

3

We can’t declare interface method with the following modifiers.

Publicàprivate, protected

Abstract->final, static, synchronized, native, strictfp

There are no restrictions on Abstract class method modifiers.

4

Every variable present inside interface is always public, static and final.

The variable present inside Abstract class need not be public static and final.

 

5

For interface variable, we must perform initialization at the time of declaration otherwise we will get compile time error.

For Abstract class variables, it is not required to perform initialization at the time of declaration.

6

Inside interface we can’t declare instance and static blocks, otherwise we will get compile error.

Inside Abstract class we can declare instance and static blocks.

7

Insider interface we can’t declare constructors.

Inside Abstract class we can declare constructor, which will be executed at the time of child object creation.

Interface VS Abstract class VS Concrete class?

Answer:
If we don’t know anything about implementation just we have requirement specification (100% Abstraction) then we should go for Interface.
Example: Servlet

If we talking about implementation but not completely (partial implementation) then we go for Abstract class.

Example: GenericServlet and HTTPServlet.
If we are talking about implementation completely and ready to provide service then we should go for concrete class.
Example: MyOwnServlet.

When to use String, StringBuffer and StringBuilder?

Answer:

When the content is fixed and won’t change frequently then we should go for String.

If the content is not fixed and keep on changing but Thread Safety is required then we should go for StringBuffer.

If the content is not fixed and keep on changing and thread safety is not required then we should go for StringBuilder.

What is the Difference between String and StringBuffer

 Answer:

String: Once we create a String Object we can't perfrom any changes
in the existing object. If we try to perfrom any changes with
those changes a new object will be created. This non-changeable
nature is called Immutability of String object.

StringBuffer: Once we create a StringBuffer object, we can perform
any type of changes in the existing object. This changeable nature
of StringBuffer object is known as mutability of StringBuffer.

What is the difference between == operator and .equals() method?


Answer: In general we use == operator for reference comparison,
whereas .equals() method for content comparison.
e.g.
String s1 = new String(“Java”);                                  
String s2 = new String(“Java”);
System.out.println(s1 == s2); //false                                      
System.out.println(s1.equals(s2)); //true                  
Both s1 and s2 refer to different memory locations. Hence both
References are different.
Note:
.equals() method present in object class also meant for reference comparison only
based on our requirement, we can override for content comparison
In String class, all wrapper classes and all collection classes,
 .equals() method is overridden for content comparison.

What is the difference between final finally and finalize?

Answer:
final:
1.Final is a modifier applicable for classes, methods and variables.
If a class declared as final then we can’t extend that class.
i.e. We can’t create child class for that class.
2.If a method is declared as final then we can’t override that method in the
child class.
3.If a variable declared as final then it will become constant and
we can’t perform re-assignment for that variable.
finally:
1.Finally is a block always associated with try catch to maintain
cleanup code
Try {
//risky code
}
Catch(X e) {
//handling code
}
Finally {
//cleanup code
}
 
finalise():
finalise() is a method which is always invoked by garbage collector
 
Note:
Finally meant for cleanup activities related to try block,
whereas finalize() meant for cleanup activities related to object just
before destroying an object to perform cleanup activities.

Calculating Square of a number Using Lambda Expression

 

//without lambda expression
 interface Square {
    public int mul(int x);
}

class Demo1 implements Square {
    public int mul(int x) {
        return x * x; // or (int x)->x*x;
    }
}

public class Multiply {
    public static void main(String[] args) {
        Square s = new Demo1();
        System.out.println("The square is : " + s.mul(8));
    }
}
//with Lambda Expression
interface Square {
    public int mul(int x);
}
public class Multiply {
    public static void main(String[] args) {
        Square s = x -> {
            return x * x;
        };
        System.out.println("The square is :" + s.mul(5));
        s.mul(5);
    }
}

Calculating Sum of Two Numbers Using Lambda Expression

 

//without Lambda Expression

interface Interf {
    public void sum(int a, int b);
}

class Demo implements Interf {
    public void sum(int a, int b) {
        System.out.println("The sum is : " + (a + b));
    }
}

public class Sum {
    public static void main(String[] args) {
        Interf i = new Demo();
        i.sum(10, 25);
    }
}

//with Lambda Expression

interface Interf {
    public void sum(int a, int b);
}

class Add {
    public static void main(String[] args) {
        Interf i = (a, b) -> System.out.println("The sum : " + (a + b));
        i.sum(10, 15);
    }
}

Lambda Expression


 What is Lambda Expression (λ):

1.Lambda expression is a new and important feature of Java
which was included in Java SE 8.
2.It provides a clear and concise way to represent one method interface
using an expression.
3.The Lambda expression is used to provide the implementation of an interface
  which has functional interface.
4.It saves a lot of code. In case of lambda expression,
5.we don't need to define the method again for providing the implementation.
6.Java lambda expression is treated as a function,
so compiler does not create .class file.


7.Lambda Expression is just an anonymous(nameless) function.
That means the function which doesn’t have the
 1.name,
 2.return type and
 3.access modifiers.

8.Lambda Expression also known as anonymous functions or closures.

Benefits of Using Lambda Expression are :

1.To provide the implementation of Functional interface.
2.Less coding.

Example:1

Without using Lambda Expression
public void method1() {
System.out.println(“hello”);
}
Converting the program into Lambda Expression :
()->{System.out.println("Hello");}
                OR
()-> System.out.println("Hello");


Example : 2

Without Lambda Expression
public void sum(inta, int b) {
System.out.println(a+b);
}
Implementing Lambda Expression :
(int a, int b)-> System.out.println(a+b);

If the type of the parameter can be decided by compiler automatically
based on the context then we can remove types also.
The above Lambda expression we can rewrite as :
(a,b) -> System.out.println(a+b);


Example : 3

Without Lambda Expression
public String str(String str) {
return str;
}
Implementing Lambda Expression :
(String str)->return str;
            OR
 (str)-> str;

Note :

1) A lambda expression can have zero or more number of arguments.
For Example
() -> System.out.println(“hello”); (zero Argument)
(int a ) -> System.out.println(a); (One Argument)
(int a, int b) -> return a+b;  (Two arguments)

2) Usually we can specify type of parameter. If the compiler expect the type based
on the context then we can remove type.
For Example :
(int a, int b) -> System.out.println(a+b);
(a,b) -> System.out.println(a+b); (Removed type of the parameters)

3) If multiple parameters present then these parameters should be separated
with comma(,).

4) If zero number of parameters available then we have to use
empty parameter [ like ()].
Ex:
() -> System.out.println(“hello”);

5) If only one parameter is available and if the compiler can expect the type
then we can remove the type and parenthesis also.
Ex:
(int a) -> System.out.println(a);
(a)-> System.out.println(a);
A -> System.out.println(a);

6) Similar to method body lambda expression body also can contain
multiple statements. if more than one statements present then we have to
enclose inside within curly braces. if one statement present then curly braces are
optional.


7) Once we write lambda expression we can call that expression just like a method,
for this functional interfaces are required.

Google Script for Data Entry Form in Google Spreadsheet

// function to validate the entry made by user in user form function validateEntry (){ // declare a variable and referernece of active goog...