1.Overview
Streams are introduced in Java in Stream API to handle the operations on the list of elements in functional programming approach.
Using Streams we can do multiple operations on the list of elements.
We can do filtering(Filter) or ordering (Map) etc., on the list of elements/collections in the stream.
Second is we can do counting/Sum/Average (Reduce)etc., on the filtered/ordered list of elements/collections.
Hence Streams works on the [Map Reduce] transformations over the collections. Below is the simple syntax of the streams.
1 2 3 4 |
<i> Stream listStream = list.stream(); listStream.filter( ..Predicate...).map(..FunctionMapper...).count(); </i> |
1.1 Streams Filter
1.1.1. stream.filter() Native Java Implementation
Below example shows how streams use Predicate Object as an argument for filter() method to filter the collections.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import java.util.List; import java.util.function.Predicate; import java.sql.SQLException; import java.util.ArrayList; public class StreamExample { public static void main(String[] args) { List<String> chatBotList = new ArrayList(); chatBotList.add("Siri"); chatBotList.add("Watson"); chatBotList.add("GoogleAssistant"); chatBotList.add("Alice"); /*****Normal Java Stream Filter Predicate Code*********/ long totalChatBotCount = chatBotList.stream().filter( new Predicate<String>() { @Override public boolean test(String t) { if(t.toUpperCase().indexOf("L")>=0) { return true; } return false; } }).count(); System.out.println("Number of ChatBots contains Word \"L\" in its Name:"+ totalChatBotCount); } } |
Result
1 |
Number of ChatBots contains Word "L" in its Name:2 |
1.1.2. stream.filter() Lambda Java Implementation
Below is the example of how Streams using Predicate as an argument for filter() method to filter the collections using lambda operations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.List; import java.util.function.Predicate; import java.sql.SQLException; import java.util.ArrayList; public class StreamExample { public static void main(String[] args) { List<String> chatBotList = new ArrayList(); chatBotList.add("Siri"); chatBotList.add("Watson"); chatBotList.add("GoogleAssistant"); chatBotList.add("Alice"); /*****Java Stream Filter Predicate Code using Lambda Saves Developers Effort*********/ // Implementing the above Code Using Lambda Operations: // Reduce the entire code of Predicate into single line by using Lambda Operations. long totalChatBotCountByLambda = chatBotList.stream().filter(t->t.toUpperCase().indexOf("L")>=0).count(); System.out.println("Number of ChatBots contains Word \"L\" in its Name:"+ totalChatBotCountByLambda); } } |
Result
1 |
Number of ChatBots contains Word "L" in its Name:2 |
1.2 Streams Map
1.1.1.stream.map() Native Implementation
Below example shows how streams use Function Object as an argument for map() method to do operation over the collections, and return the collections.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import java.util.ArrayList; import java.util.List; import java.util.function.Function; import java.util.stream.Collectors; public class StreamsMapExample { public static void main(String args[]) { List<String> numbersList = new ArrayList(); numbersList.add("12"); numbersList.add("9000"); numbersList.add("123456789"); numbersList.add("943257"); /** With Native Implementation -> Converting all numbers to length of 10 digits **/ List resultList = numbersList.stream().map(new Function<String, String>() { @Override public String apply(String t) { if(t.length()<10) { //Appending zero before to number if length<10 int gap = 10-t.length(); for(int i=0;i<gap;i++) { t="0"+t; } } return t; } }).collect(Collectors.toList()); System.out.println("LowerCase Result List:" + resultList); } } |
Result
1 |
LowerCase Result List:[0000000012, 0000009000, 0123456789, 0000943257] |
1.1.2. stream.map()- Lambda Implementation
Below example shows how streams use Function Object as an argument for map() method to do operation over the collections, and return the collections.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import java.util.ArrayList; import java.util.List; import java.util.function.Function; import java.util.stream.Collectors; public class StreamsMapExample { public static void main(String args[]) { List<String> numbersList = new ArrayList(); numbersList.add("12"); numbersList.add("9000"); numbersList.add("123456789"); numbersList.add("943257"); /** With Lambda Implementation -> Converting all numbers to length of 10 digits **/ List lambdaStreamResult = numbersList.stream().map((Function<String, String>) (k)->{ if(k.length()<10) { //Appending zero before to number if length<10 int gap = 10-k.length(); for(int i=0;i<gap;i++) { k="0"+k; } } return k; }).collect(Collectors.toList()); System.out.println("LowerCase Result List:" + lambdaStreamResult); } } |
Result
1 |
LowerCase Result List:[0000000012, 0000009000, 0123456789, 000943257] |