vasanth0989 / prep-myself

0 stars 0 forks source link

Functional Interfaces #10

Open vasanth0989 opened 5 months ago

vasanth0989 commented 5 months ago
public class TestMain {

    public static void main(String[] args) throws Exception {
        // Introducing Java 8 Functional Interface & Lambda Expressions
        // An Interface is called as an Functional interface if it has one single abstract method.
        // Anonymous  class in Java
        Shape circle = new Shape() {
            @Override
            public int sides() {
                return 0;
            }
        };
        // Multi line you have to use {}
        Shape square = () -> {
            int num1 = 2;
            int num2 = 2;
            return 2 + 2;
        };
        // Java 8 feature which is the Lambda expression.
        // if your expression is going to be single line you don't need {}
        Shape polygon = () -> 5;
        System.out.println(polygon.version());
        System.out.println(square.sides());
        System.out.println(square.version());
        System.out.println(Shape.customClassName());
        // Functional interface can contain only one abstract method
        // But it can contain n number of default and static methods
    }
}

interface Shape {
    // single abstract method
    int sides();

    default String version() {
        return "Using JDK 17";
    }

    static String customClassName() {
        return Shape.class.getName();
    }

}

class Circle implements Shape {
    @Override
    public int sides() {
        return 0;
    }
}
vasanth0989 commented 4 months ago
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

public class TestMain {

    public static void main(String[] args) throws Exception {
        // Introducing Java 8 Functional Interface & Lambda Expressions
        // An Interface is called as an Functional interface if it has one single abstract method.
        // Anonymous  class in Java
        Circle circleA = new Circle();

        // Pre java 8
        Shape circle = new Shape() {
            @Override
            public int sides(int num1, int num2) {
                return 0;
            }
        };
        // From Java 8 we can use Lambda expression
        // With lambda expression you are just providing the implementation for the single abstract method declared in the interface.
        // () -> {}

        Shape square = (i, j) -> i * j;
        int output = square.sides(2,3);
        System.out.println("Val:"+output);

        // There are built in functional interfaces within Java
        // Java ships with those functional interfaces.
        // Predicate
        // Function
        // Consumer
        // Supplier

        // Requirement create a predicate to test for Employee age is greater than 18.
        Employee employee1 = new Employee("John", 28);
        Employee employee2 = new Employee("Smith", 17);

        // Predicate takes in one input and returns a boolean value
        Predicate<Employee>  empAgeCheckPredicate = emp -> emp.getAge() > 18;
        System.out.println("Emp1:"+empAgeCheckPredicate.test(employee1));
        System.out.println("Emp2:"+empAgeCheckPredicate.test(employee2));

        //Function
        // Takes in an input and produce an output value
        Function<String, Integer> intConversionFunction = s -> Integer.valueOf(s);

//        int apply(String val) {
//            return Integer.valueOf(val);
//        }

        int val = intConversionFunction.apply("78");
        System.out.println(val);

        // Consumer
        // It will take an input but produces nothing
        Consumer<String> formatString = s -> System.out.println("Formatedd Value:"+s);
        formatString.accept("Hello world");

        // Supplier
        // It will zero input but produces one output
        Supplier<Employee> employeeSupplier = () -> new Employee("John",25);
        System.out.println(employeeSupplier.get());

    }
}

class Employee {
   private String name;
   private int age;

    public Employee(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

@FunctionalInterface
interface Shape {
    // single abstract method
    int sides(int num1, int num2);

    // By Java 8 we can use default and static methods in interface.
    default String version() {
        return "Using JDK 17";
    }

    static String customClassName() {
        return Shape.class.getName();
    }

}

class Printer {

    void print(){
        System.out.println("From printer");
    }
}
class Circle extends Printer implements Shape {
    @Override
    public int sides(int num1, int num2) {
        return 0;
    }

    @Override
    void print() {
        System.out.println("From circle way!!");
    }
}