Why use consumer and supplier? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

Why use consumer and supplier?

Please, If anyone knows the advantage of the Consumer and the Supplier explain. I read a few websites about them in Java8 but I wasn't convinced why I should use this functions.

14th Jun 2019, 11:12 PM
Hosseinam
Hosseinam - avatar
3 Answers
+ 11
No or yes Lambada experssion remove boilerplate codes.
15th Jun 2019, 10:32 AM
Sumit Programmer😎😎
Sumit Programmer😎😎 - avatar
+ 10
Consumer Interface is a functional interface and this interface have a abstract accept method. If you want to itrate list using forEach method than forEach method want Consumer interface To print list item one by one. In simple word forEach method aceept list item and consumer interface responcibal to print item one by one using this accept method. thats all. Here is internal implementaion (java 7) v.forEach(new Consumer<Integer>(){ public void accept(Integer i){ System.out.println(i); } }); Using lambda expression (java 8) v.forEach( i ->System.out.println(i));
15th Jun 2019, 2:47 AM
Sumit Programmer😎😎
Sumit Programmer😎😎 - avatar
+ 7
Sumit Programmer😎😎 thank you for your description. Does it affect the performance of the program?
15th Jun 2019, 10:26 AM
Hosseinam
Hosseinam - avatar