Java 8 — Consumer | Code Factory

@FunctionalInterface
public interface Consumer<T> {
void accept(T t);
default Consumer<T> andThen(Consumer<? super T> after) { }
}
package com.codeFactory.consumer;import java.util.function.Consumer;public class Test {
public static void main(String... args) {
Consumer<String> c = s -> System.out.println(s);
c.accept("Code Factory");
}
}
Code Factory

Program to display Movie Information by using Consumer :

package com.codeFactory.consumer;import java.util.ArrayList;
import java.util.function.Consumer;
class Movie {
String name;
String actor;
String actress;

Movie(String name, String actor, String actress) {
this.name = name;
this.actor = actor;
this.actress = actress;
}
@Override
public String toString() {
return "Movie [name=" + name + ", actor=" + actor + ", actress=" + actress + "]";
}
}
public class Test {
public static void main(String... args) {
ArrayList<Movie> i = new ArrayList<Movie>();
populate(i);
Consumer<Movie> c = m -> System.out.println(m);
for(Movie m : i) {
c.accept(m);
}
}
private static void populate(ArrayList<Movie> i) {
i.add(new Movie("Bahubali", "Prabhas", "Anushka"));
i.add(new Movie("Rayees", "Sharukh", "Sunny"));
i.add(new Movie("Dangal", "Ameer", "Ritu"));
i.add(new Movie("Sultan", "Salman", "Anushka"));
}
}
Movie [name=Bahubali, actor=Prabhas, actress=Anushka]
Movie [name=Rayees, actor=Sharukh, actress=Sunny]
Movie [name=Dangal, actor=Ameer, actress=Ritu]
Movie [name=Sultan, actor=Salman, actress=Anushka]

Program to display Student Information by using Predicate, Function and Consumer :

package com.codeFactory.consumer;import java.util.ArrayList;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
class Student {
String name;
int marks;

Student(String name, int marks) {
this.name = name;
this.marks = marks;
}
@Override
public String toString() {
return "Student [name=" + name + ", marks=" + marks + "]";
}
}
public class Test {
public static void main(String... args) {
ArrayList<Student> i = new ArrayList<Student>();
populate(i);

Predicate<Student> p = s -> s.marks >=60;

Function<Student, String> f = s -> {
int marks = s.marks;
if(marks >= 80) {
return "A[Distinction]";
} else if(marks >= 60) {
return "B[First Class]";
} else if(marks >= 50) {
return "C[Second Class]";
} else if(marks >= 35) {
return "D[Third Class]";
} else {
return "E[Failed]";
}
};

Consumer<Student> c = s -> {
System.out.println(s + " -> " + f.apply(s));
};

for(Student s : i) {
if(p.test(s)) {
c.accept(s);
}
}
}
private static void populate(ArrayList<Student> i) {
i.add(new Student("Narendra", 100));
i.add(new Student("Amit", 65));
i.add(new Student("Vijay", 55));
i.add(new Student("Nitin", 45));
i.add(new Student("Rahul", 25));
}
}
Student [name=Narendra, marks=100] -> A[Distinction]
Student [name=Amit, marks=65] -> B[First Class]

Consumer Chaining :

package com.codeFactory.consumer;import java.util.ArrayList;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
class Movie {
String name;
String result;

Movie(String name, String result) {
this.name = name;
this.result = result;
}
}public class Test {
public static void main(String... args) {
Consumer<Movie> c1 = m -> {
System.out.println("Movie " + m.name + " is ready to release");
};
Consumer<Movie> c2 = m -> {
System.out.println("Movie " + m.name + " is just released and it is " + m.result);
};
Consumer<Movie> c3 = m -> {
System.out.println("Movie " + m.name + " information storing in the database");
};

Consumer<Movie> chainedC = c1.andThen(c2).andThen(c3);

Movie m1 = new Movie("Bahubali", "Hit");
chainedC.accept(m1);

Movie m2 = new Movie("Spider", "Flop");
chainedC.accept(m2);
}
}
Movie Bahubali is ready to release
Movie Bahubali is just released and it is Hit
Movie Bahubali information storing in the database
Movie Spider is ready to release
Movie Spider is just released and it is Flop
Movie Spider information storing in the database

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Lifting the Recurse of Factorial

Crio Winter Of Doing — My Learning Experience till now

Cocktail By Ingredient

Public Deployment of Gladios- Testnet Nodes (Second phase/third part)

The Coliving Digital Age-Managing Coliving Properties,the Futuristic Way

What Is RESTful API? How to Create and Use RESTful APIs?

EuroIA behind the scenes: Part one

DuckDuckGo, a privacy-focused search engine, will have its best year ever in 2021

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Singleton Design Pattern in Java

Method Overloading and Method Overriding in Java

Naming Conventions in Java

Fundamentals of Java