Sunday, March 18, 2018

Java 8 consumer chaining


https://www.javabrahman.com/java-8/java-8-java-util-function-consumer-tutorial-with-examples/

Consumer => accept(T t)
Consumer interface will accept some Type T, and do something on it.

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

public class ConsumerFunctionExample{
    public static void main(String args[]){
        Consumer<Integer> consumer = i-> System.out.print(" "+i);
        Consumer<Integer> consumerWithAndThen = consumer.andThen( i-> System.out.print("(printed "+i+")"));
        //  Note:
        //      i-> System.out.print("(printed "+i+")") will create a new Consumer Object, and it can be
        //      cosidered as: Consumer<Integer> consumer2 =  i-> System.out.print("(printed "+i+")")
        //      and comsumer2 will be pass to consumer.andThen()
        //      Because consumer addnThen returns another Consumer<T> object
        //        public interface Consumer<T> {
        //            void accept(T t);
        //            default Consumer<T> andThen(Consumer<? super T> after) {
        //                Objects.requireNonNull(after);
        //                return (T t) -> { accept(t); after.accept(t); };
        //            }
        //        }
        //      In this case (T t) -> { accept(t); after.accept(t); };
        //      New consumer created new accept function which include 2 operations
        //                { consumer.accept(t); consumer.andThen();}
        //                => { i-> System.out.print(" "+i), i-> System.out.print("(printed "+i+")")}

        System.out.println(consumer.hashCode());
        System.out.println(consumerWithAndThen.hashCode());
        System.out.println(consumer == consumerWithAndThen);
        List<Integer> integerList= Arrays.asList(new Integer(1),
                new Integer(10), new Integer(200),
                new Integer(101), new Integer(-10),
                new Integer(0));
        printList(integerList,consumerWithAndThen);
        System.out.println();


        // **********   Consumer Chaining  *********
        Consumer<String> consumer1 = i -> System.out.println(i);

        // Note: consumer2.accept = {consumer1.accept + consumer1.andThen}
        //       and:
        //       cosumer1.accept = i -> System.out.println(i)
        //       cosumer1.andThen = i -> System.out.println("length:" + i.length())
        Consumer<String> consumer2 = consumer1.andThen(i -> System.out.println("length:" + i.length()));

        // Note: consumer3.accept = {consumer2.accept + consumer2.andThen}
        //       and:
        //       cosumer2.accept = {consumer1.accept + consumer1.andThen}
        //       cosumer2.andThen = i -> System.out.println("Lower Case:" + i.toLowerCase()))
        Consumer<String> consumer3 = consumer2.andThen(i -> System.out.println("Lower Case:" + i.toLowerCase()));

        // Note: consumer4.accept = {consumer3.accept + consumer3.andThen}
        //       and:
        //       cosumer3.accept = {consumer2.accept + consumer2.andThen}
        //       cosumer3.andThen = i -> System.out.println("Upper Case:" + i.toUpperCase()))
        Consumer<String> consumer4 = consumer3.andThen(i -> System.out.println("Upper Case:" + i.toUpperCase()));


        // Note: consumer4.accept will call the followings: { consumer3.accept + consumer3.andThen }
        //       consumer3.accept + consumer3.andThen
        //   =>  consumer3.accept + i -> System.out.println("Upper Case:" + i.toUpperCase()))
        //   =>  {consumer2.accept + consumer2.andThen}
        //       + i -> System.out.println("Upper Case:" + i.toUpperCase()))
        //   =>  {consumer1.accept + consumer1.andThen}
        //       + i -> System.out.println("Lower Case:" + i.toLowerCase()))       //  cosumer2.andThen
        //       + i -> System.out.println("Upper Case:" + i.toUpperCase()))       //  cosumer3.andThen
        //   =>  i -> System.out.println(i)                                        //  cosumer1.accept
        //       + i -> System.out.println("length:" + i.length())                 //  cosumer1.andThen
        //       + i -> System.out.println("Lower Case:" + i.toLowerCase()))       //  cosumer2.andThen
        //       + i -> System.out.println("Upper Case:" + i.toUpperCase()))       //  cosumer3.andThen
        consumer4.accept("Andy");

    }
    public static void printList(List<Integer> listOfIntegers, Consumer<Integer> consumer){
        for(Integer integer:listOfIntegers){
            consumer.accept(integer);
        }
    }
}

No comments:

Post a Comment

java special for collection size, array size, and string size

Size: For Collections (eg: Map, List, etc ): usually it use collection.size(), eg         Map<Character, Integer> map ...