the StreamAPI is used toprocess collections
of objects. A stream is a sequence of objects
that supports various methods which can be
pipelined toproduce the desired result.
It is very similar tobuild().perform()
method in Actionsclass. It is chaining
some operations in an order.
The features of Java stream are:-A stream is not a data structure instead
it takes input from the Collections,Arrays or I/O channels.-Streams don’t change the original data
structure, they only provide the result
as per the pipelined methods.-Each intermediate operation is lazily
executed and returns a stream as a result,
hence various intermediate operations
can be pipelined. Terminal operations mark
the end of the stream and return the result.
DifferentOperationsOnStreams:
map ==>The map method is used toreturn a
stream consisting of the results of applying
the given function tothe elements of thisstream.
List number =Arrays.asList(2,3,4,5);List square = number.stream().map(x->x*x).collect(Collectors.toList());
filter ==>The filter method is used toselect elements as per the Predicate passed as argument.
List names =Arrays.asList("Reflection","Collection","Stream");List result = names.stream().filter(s->s.startsWith("S")).collect(Collectors.toList());
sorted ==>The sorted method is used tosort the stream.
List names =Arrays.asList("Reflection","Collection","Stream");List result = names.stream().sorted().collect(Collectors.toList());ArrayList<String> list3 =newArrayList(Arrays.asList("monkey","donkey","onion"));List<String> list4 = list3.stream().filter(each->!"onion".equals(each)).collect(Collectors.toList());System.out.println(list4);staticStringreverse(String word){returnArrays.stream(word.split("")).reduce("",(x,y)-> y+x );}
// Creates a list of ProgrammingLanguage objects,// maps it with a method of ProgrammingLanguage to get the name// and filters them for programming languages, which are not starting with C.importjava.util.List;importjava.util.Arrays;importjava.util.stream.Collectors;classProgrammingLanguage{privateString name ="";publicstaticvoidmain(finalString args[]){List<ProgrammingLanguage> programmingLanguages =Arrays.asList(newProgrammingLanguage("Java"),newProgrammingLanguage("Python"),newProgrammingLanguage("C#"),newProgrammingLanguage("JS"),newProgrammingLanguage("C++"),newProgrammingLanguage("C"));List<String> nonCProgrammingLanguages = programmingLanguages.stream()// creates stream from list.map(ProgrammingLanguage::getName).filter(name ->!name.startsWith("C")).collect(Collectors.toList());// convert stream back to listSystem.out.println("Names not starting with C: "+ nonCProgrammingLanguages);}publicProgrammingLanguage(finalString name){setName(name);}publicStringgetName(){return name;}publicvoidsetName(finalString name){this.name = name;}}
//a simple program to demonstrate the use of stream in javaimportjava.util.*;importjava.util.stream.*;classDemo{publicstaticvoidmain(String args[]){// create a list of integersList<Integer> number =Arrays.asList(2,3,4,5);// demonstration of map methodList<Integer> square = number.stream().map(x -> x*x).collect(Collectors.toList());System.out.println(square);// create a list of StringList<String> names =Arrays.asList("Reflection","Collection","Stream");// demonstration of filter methodList<String> result = names.stream().filter(s->s.startsWith("S")).collect(Collectors.toList());System.out.println(result);// demonstration of sorted methodList<String> show =
names.stream().sorted().collect(Collectors.toList());System.out.println(show);// create a list of integersList<Integer> numbers =Arrays.asList(2,3,4,5,2);// collect method returns a setSet<Integer> squareSet =
numbers.stream().map(x->x*x).collect(Collectors.toSet());System.out.println(squareSet);// demonstration of forEach method
number.stream().map(x->x*x).forEach(y->System.out.println(y));// demonstration of reduce methodint even =
number.stream().filter(x->x%2==0).reduce(0,(ans,i)-> ans+i);System.out.println(even);}}
importjava.util.*;publicclassMain{publicstaticvoidmain(String[] args){List<Integer> list =Arrays.asList(1,2,3,4,5);
list.stream().map(x -> x +1).forEach(System.out::println);}}