February 12th, 2024
Functional Interfaces

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

				
					package com.brains.reactivejavademo;

import java.time.LocalDateTime;
import java.util.concurrent.Callable;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

public class T1 {

    private static Callable<String> callable(){
        return () -> "Callable: hello";
    }
    private static Runnable runnable = () -> System.out.println("Runnable: This is runnable");
    private static Supplier<String> supplier = () -> "Supplier";
    private static Consumer<String> consumer(){
        return s -> System.out.println("Consumer : "+s);
    }
    private static Function<String, String> func(){
        return (s) -> s.concat(String.valueOf(s.length()));
    }
    private static BiFunction<String, String, String> bifunc(){
        return (s,r) -> s.concat(r);
    }

    public static void main(String[] args) throws Exception {

        // callable
        Callable<String> s = callable();
        System.out.println(s.call());
        //-- Callable: hello

        // runnable
        runnable.run();
        //-- Runnable: This is runnable

        String s1 = supplier.get();
        System.out.println(s1);
        //-- Supplier

        // consumer
        consumer().accept("abc");
        //-- Consumer : abc

        String function = func().apply("hello");
        System.out.println(function);
        //-- hello5

        String bifunction = bifunc().apply("helo", "world");
        System.out.println(bifunction);
        //-- heloworld
    }
}

				
			

Leave a Reply

Your email address will not be published. Required fields are marked *