Java 8 – Consumer | Code Factory


Index Page : Link

Donate : Link

Medium Blog : Link

Applications : Link

Sometimes our requirment is we have to provide some input value, perform certain operation, but not required to return anything, then we should go for Consumer .i.e Consumer can be used to consume object and perform certain operation.

Consumer Functional Interface contains one abstract method accept.

@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");
	}
}

Output :

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"));
	}
}

Output :

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));
	}
}

Output :

Student [name=Narendra, marks=100] -> A[Distinction]
Student [name=Amit, marks=65] -> B[First Class]

. . . . .

Consumer Chaining :

Just like Predicate Chaining and Function Chaining, Consumer Chaining is also possible. For this Consumer Functional Interface contains default method andThen().

c1.andThen(c2).andThen(c3).accept(s)

First Consumer c1 will be applied followed by c2 and c3.

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);
	}
}

Output :

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

2 thoughts on “Java 8 – Consumer | Code Factory”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s