Skip to content

Commit 4013407

Browse files
committed
Add andThen method implementation
1 parent 16c73dc commit 4013407

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

README.md

+38-1
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,42 @@ void accept(T t);
5252
default Consumer<T> andThen(Consumer<? super T> after);
5353
```
5454
- The ```accept``` method is the Single Abstract Method (SAM) which accepts a single argument of type T.
55-
- ```andThen``` is a default method used for composition.
55+
- ```andThen``` is a default method used for composition or chaining.
56+
- ``` andThen``` takes a consumer as input and returns a consumer.
57+
58+
#### ``` accept ``` method example
59+
60+
61+
```
62+
public static void printUsingAcceptMethod(){
63+
// Create a function that prints what ever is passed through accept method of consumer interface.
64+
Consumer<String> printConsumer = t -> System.out.println(t);
65+
Stream<String> cities = Stream.of("Sydney", "Dhaka", "New York", "London");
66+
// foreach method of stream interface takes a consumer through parameter.
67+
cities.forEach(printConsumer);
68+
}
69+
70+
```
71+
72+
### ```andThen``` method example.
73+
74+
```
75+
public void printUsingAndTherMethod(){
76+
List<String> cities = Arrays.asList("Sydney", "Dhaka", "New York", "London");
77+
78+
// This consumer converts an string list into upper case.
79+
Consumer<List<String>> upperCaseConsumer = list -> {
80+
for(int i=0; i< list.size(); i++){
81+
list.set(i, list.get(i).toUpperCase());
82+
}
83+
};
84+
85+
// This Consumer print list of string
86+
Consumer<List<String>> printConsumer = list -> list.stream().forEach(System.out::println);
87+
88+
// Chaining consumers using andThen
89+
upperCaseConsumer.andThen(printConsumer).accept(cities);
90+
}
91+
92+
```
5693

src/com/msi/consumer/Main.java

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.msi.consumer;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.function.Consumer;
6+
import java.util.stream.Stream;
7+
8+
public class Main {
9+
10+
public static void main(String[] args) {
11+
printUsingAcceptMethod();
12+
printUsingAndTherMethod();
13+
14+
}
15+
16+
public static void printUsingAcceptMethod(){
17+
18+
// Create a function that prints what ever is passed through accept method of consumer interface.
19+
Consumer<String> printConsumer = t -> System.out.println(t);
20+
21+
// Create a stream of string
22+
Stream<String> cities = Stream.of("Sydney", "Dhaka", "New York", "London");
23+
24+
// foreach method of stream interface takes a consumer through parameter.
25+
cities.forEach(printConsumer);
26+
}
27+
28+
public static void printUsingAndTherMethod(){
29+
List<String> cities = Arrays.asList("Sydney", "Dhaka", "New York", "London");
30+
31+
// This consumer converts an string list into upper case.
32+
Consumer<List<String>> upperCaseConsumer = list -> {
33+
for(int i=0; i< list.size(); i++){
34+
list.set(i, list.get(i).toUpperCase());
35+
}
36+
};
37+
38+
// This Consumer print list of string
39+
Consumer<List<String>> printConsumer = list -> list.stream().forEach(System.out::println);
40+
41+
// Chaining consumers using andThen
42+
upperCaseConsumer.andThen(printConsumer).accept(cities);
43+
}
44+
45+
}

0 commit comments

Comments
 (0)