Click here to watch in Youtube :
https://www.youtube.com/watch?v=MsnC20IUGFg&list=UUhwKlOVR041tngjerWxVccw
StreamDemo1.java
import java.util.ArrayList; import java.util.List; /** * * Intermediate operations return the stream itself so you can chain * multiple method calls in a row. * */ public class StreamDemo1 { public static void main(String[] args) { List<String> nameList = new ArrayList<>(); nameList.add("Amitabh"); nameList.add("Shekhar"); nameList.add("Aman"); nameList.add("Rahul"); nameList.add("Shahrukh"); nameList.add("Salman"); nameList.add("Yana"); nameList.add("Lokesh"); /* * Filter accepts a predicate to filter all elements of the * stream. This operation is intermediate which enables us to * call another stream operation (e.g. forEach) on the result. */ nameList.stream().filter((s) -> s.startsWith("A")) .forEach(System.out::println); } }
Amitabh AmanStreamDemo2.java
import java.util.ArrayList; import java.util.List; /** * * Intermediate operations return the stream itself so you can chain * multiple method calls in a row. * */ public class StreamDemo2 { public static void main(String[] args) { List<String> nameList = new ArrayList<>(); nameList.add("Amitabh"); nameList.add("Shekhar"); nameList.add("Aman"); nameList.add("Rahul"); nameList.add("Shahrukh"); nameList.add("Salman"); nameList.add("Yana"); nameList.add("Lokesh"); /* * The intermediate operation map converts each element into * another object via the given function. The following * example converts each string into an upper-cased string. * But you can also use map to transform each object into * another type. */ nameList.stream().filter((s) -> s.startsWith("S")) .map(String::toUpperCase) .forEach(System.out::println); } }Output
SHEKHAR SHAHRUKH SALMANStreamDemo3.java
import java.util.ArrayList; import java.util.List; /** * * Intermediate operations return the stream itself so you can chain * multiple method calls in a row. * */ public class StreamDemo3 { public static void main(String[] args) { List<String> nameList = new ArrayList<>(); nameList.add("Amitabh"); nameList.add("Shekhar"); nameList.add("Aman"); nameList.add("Rahul"); nameList.add("Shahrukh"); nameList.add("Salman"); nameList.add("Yana"); nameList.add("Lokesh"); /* * Sorted is an intermediate operation which returns a sorted * view of the stream. The elements are sorted in natural * order unless you pass a custom Comparator. * * Keep in mind that sorted does only create a sorted view of * the stream without manipulating the ordering of the backed * collection. The ordering of memberNames is untouched. */ nameList.stream().sorted().map(String::toUpperCase) .forEach(System.out::println); } }Output
AMAN AMITABH LOKESH RAHUL SALMAN SHAHRUKH SHEKHAR YANAClick the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_im_op_examples_App.zip?attredirects=0&d=1
Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/StreamDemo_im_op_examples_App
Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/bc0205890e206d66c03b068b168e05c78f68f50d/BasicJava/StreamDemo_im_op_examples_App/?at=master
See also:
No comments:
Post a Comment