Java 8 consumer, predicate and supplier with streams
Posted By : Mohit Gupta | 28-Jun-2021
Consumer functional interface
*Consumer <T> is an in-built functional interface introduced in java 8 consumer can be used in all contexts where an object needs to be consumed i.e taken as input, and a few operations are to be performed on the object without returning any results.
performs this operation on the give argument
@param t the input argument
void accept(T t);
public class ConsumerDemo {
public static void main(String[] args) {
/*
* Consumer<Integer> consumer = t -> System.out.println("Printing : " + t);
*
* consumer.accept(10);
*/
List<Integer> list1 = Arrays.asList(1, 2, 3, 4, 5);
list1.stream().forEach(t -> System.out.println("print : " + t));
}
}
Predicate functional interface
This functional interface used for conditional check
where you think ,we can use these true/false returning functions in day to day programing we should choose predicacte
Evaluates this predicates on the given argument
@param t the input argument
boolean test (T t);
EXAMPLE:-
public class PredicateDemo {
public static void main(String[] args) {
List<Integer> list1 = Arrays.asList(1, 2, 3, 4, 5);
list1.stream().filter(t -> t % 2 == 0).forEach(t -> System.out.println("print Even: " + t));
}
}
Supplier functional interface
Supplier can be used in all context where there is no input but an output is expected.
gets a result
@return a result
T get();
EXAMPLE:-
public class SupplierDemo {
public static void main(String[] args) {
List<String> list1 = Arrays.asList();
System.out.println(list1.stream().findAny().orElseGet(() -> "Hi viewers"));
}
Steams in java
* Stream api is used to process the collection of objects
A stream is a sequence of objects that supports various methods that can be pipelined to produce the desired outcome or result.
It is not a data structure as it takes input from the collections arrays or I/O channels.
Stream doesn't change the original data structure, they only deliver the result as per the pipelined methods.
why we need stream?
1 functional programing
2 code reduce
3 bulk operation
for Each method Example:-
public class ForEachDemo {
// filter----> conditional check
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Murrit");
list.add("john");
list.add("piter");
list.add("marek");
list.add("mac");
for (String s : list) {
if (s.startsWith("m")) {
System.out.println(s);
}
}
list.stream().filter(t -> t.startsWith("m")).forEach(t -> System.out.println(t));
Map<Integer, String> map = new HashMap<>();
map.put(1, "a");
map.put(2, "b");
map.put(3, "c");
map.put(4, "d");
/*
* map.forEach((key,value)->System.out.println(key+": "+value));*/
map.entrySet().stream().filter(k->k.getKey()%2==0).forEach(obj->System.out.println(obj));
/*
* Consumer<String> consumer=(t)->System.out.println(t); for(String s1:list) {
* consumer.accept(s1); }
*/
}
}
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Mohit Gupta
Mohit Gupta is working as a Developer, having good knowledge of core java. he is good in behaviour and and prefers to complete assigned tasks on time.