This Blog explains about Java 8 – New Features – default, static Methods in Interface, Predicates.
Ticket booking. java
package default_static_demo;
import java.util.ArrayList;
import java.util.Collection;
import java.util.function.Predicate;
public class Ticket_Booking {
public static void main(String[] args) {
// TODO Auto-generated method stub
// Predicate<Integer> p = no -> no>60;
// System.out.println(p.test(55));
// Predicate<String> p = name -> (name.length()>=8);
// System.out.println(p.test("abcd12"));
// ArrayList al = new ArrayList();
// al.add(10);
// System.out.println(al.size());
// Predicate<Collection> p = aa -> aa.size()>0;
// System.out.println(p.test(al));
//Predicate in Array
int[] ar = {10,11, 13,4,5,6,7,12};
Predicate<Integer> p1 = no -> no%2!=0;
Predicate<Integer> p2 = no -> no>10;
// for(int i=0; i<ar.length; i++)
// {
// System.out.println(p.test(ar[i]));
// }
// && || !
//negate !
//or
//add
//check(p1.negate(),ar);
//check(p1.negate(), ar);
//check(p1.and(p2),ar);
check(p1.or(p2), ar);
}
// public boolean Test(int age)
// {
// if(age>60)
// return true;
// else return false;
// }
private static void check(Predicate<Integer> p, int[] ar) {
// TODO Auto-generated method stub
for(int i=0; i<ar.length; i++)
{
boolean result = p.test(ar[i]);
if(result)
System.out.println(ar[i]);
}
}
}
Employee.java
package default_static_demo;
public class Employee implements
Contract{
public static void main(String[] args) {
Employee emp = new Employee();
//emp.calculate_salary(1000, 200);
Contract.test();
}
@Override
public void calculate_salary(int a, int b) {
// TODO Auto-generated method stub
//Contract2.super.calculate_salary(a, b);
Contract.super.calculate_salary(a, b);
}
}
Contract.java
package default_static_demo;
public interface Contract {
default void calculate_salary(int a, int b)
{
System.out.println(a+b);
}
static void test()
{
System.out.println("Contract Interface");
}
}
Contract2.java
package default_static_demo;
public interface Contract2 {
default void calculate_salary(int a, int b)
{
System.out.println(a-b);
}
}