We got 4 new Functional Interfaces in Java8, which got very popular because of
their extensive use in Streams & Lambda.
We have see 2 from them - Function & Predicate
Here I am putting other 2 together as they don't have much to discuss or show, i.e.
Consumer & Supplier.
A quick glance on the main methods of these interfaces-
Function : R apply(T), it takes some input T return some result R
Predicate : boolean test(T), it takes some input & test it against the configured condition
& returns True or False as boolean value.
Supplier : T get(), it is a simple sweet guy, who doesn't take anything but gives
you something whenever you call it. Though I feel that method name doesn't justity
its behavior, it should have been something like supply().
Consumer : void accept(T), it is some greedy guy who just knows to take but doesn't give
anything back. And this accept() method name justifies its nature also.
So below I have given simple sample to check Consumer & Supplier.
Note: andThen() method in consumer takes another consumer to process & as shown below.
accept() takes any expression or method call or some variable which provides some
concrete value, as shown below I am making use of supplier to provide to get the value
& provide that to consumer. Other flavors of these interfaces you can check yourself as
their concepts are same.
their extensive use in Streams & Lambda.
We have see 2 from them - Function & Predicate
Here I am putting other 2 together as they don't have much to discuss or show, i.e.
Consumer & Supplier.
A quick glance on the main methods of these interfaces-
Function : R apply(T), it takes some input T return some result R
Predicate : boolean test(T), it takes some input & test it against the configured condition
& returns True or False as boolean value.
Supplier : T get(), it is a simple sweet guy, who doesn't take anything but gives
you something whenever you call it. Though I feel that method name doesn't justity
its behavior, it should have been something like supply().
Consumer : void accept(T), it is some greedy guy who just knows to take but doesn't give
anything back. And this accept() method name justifies its nature also.
So below I have given simple sample to check Consumer & Supplier.
Note: andThen() method in consumer takes another consumer to process & as shown below.
accept() takes any expression or method call or some variable which provides some
concrete value, as shown below I am making use of supplier to provide to get the value
& provide that to consumer. Other flavors of these interfaces you can check yourself as
their concepts are same.
Consumers