Streams
To process objects of the collection, in 1.8 version
Streams concept introduced.
What is the differences between java.util.streams and java.io streams?
java.util streams meant for processing objects
from the collection. Ie, it represents a stream of objects from the collection but java.io streams
meant for processing binary and character data with respect
to file. i.e it represents stream of binary
data or character data from the file .hence
java.io streams and java.util streams
both are different.
What is the difference between collection and stream?
if we want to represent a group of individual objects
as a single entity then we should go for collection.
if we want to process
a group of objects from the collection then we should go for streams.
we can create
a stream object
to the collection by using
stream()method of Collection interface. stream() method
is a default method added
to the Collection in 1.8 version.
default Stream stream()
Ex:
Stream s = c.stream();
Stream is an
interface present in java.util.stream.
once we got the stream,
by using that we can process objects
of that collection. we can process the objects in the following two phases
1.configuration 2.processing
configuration:
we can configure either by using filter mechanism or
by using map mechanism.
Filtering:
we can configure
a filter to filter elements
from the collection based on some boolean condition
by using filter()method of Stream interface.
public Stream
filter(Predicate<T> t)
here (Predicate<T > t
) can be a boolean valued function/lambda expression
Ex:
Stream
s=c.stream();
Stream
s1=s.filter(i -> i%2==0);
Hence
to filter elements of collection based
on some booleancondition we should go for
filter()method.
Mapping:
If we want
to create a separate new object, for every object
present in the collection based
on our requirement then we should go for map () method
of Stream interface.

public Stream map
(Function f);
It can be lambda expression also
Ex:
Stream s = c.stream(); Stream s1 = s.map(i-> i+10);
Once we performed configuration we can process objects
by using several
methods. 2.Processing
processing by collect() method Processing by count()method
Processing by sorted()method Processing by min() and
max() methods forEach() method
toArray() method Stream.of()method
processing by collect() method
This method collects
the elements from
the stream and adding
to the specified to the collection
indicated (specified)by argument.
Ex:1
To collect only even numbers
from the array
list Approach-1: without Streams
II. Processing by count()method
this method returns
number of elements
present in the stream. public
long count()
Ex:
long count=l.stream().filter(s
->s.length()==5).count(); sop(“the number of 5 length strings is:”+count);
II.
Processing
by sorted()method
if we sort the
elements present inside stream then we should go for sorted() method.
the sorting can either default
natural sorting order
or customized sorting
order specified by comparator.
sorted()-default natural sorting order sorted(Comparator c)-customized sorting order.
Ex:
List<String>
l3=l.stream().sorted().collect(Collectors.toList()); sop(“according to default natural sorting
order:”+l3);
List<String>
l4=l.stream().sorted((s1,s2) ->
-s1.compareTo(s2)).collect(Collectors.toList()); sop(“according
to customized sorting order:”+l4);
III. Processing by min() and max() methods
min(Comparator c)
returns minimum
value according to specified comparator.
max(Comparator c)
returns maximum
value according to specified comparator
Ex:
String min=l.stream().min((s1,s2) ->
s1.compareTo(s2)).get(); sop(“minimum value is:”+min);
String max=l.stream().max((s1,s2) ->
s1.compareTo(s2)).get(); sop(“maximum value is:”+max);
IV.forEach() method
this method will
not return anything.
this
method will take
lambda expression as argument and
apply that lambda
expression for each element present
in the stream.
Ex:
l.stream().forEach(s->sop(s)); l3.stream().forEach(System.out::
println);
EX:
VI.
toArray() method
we can use
toArray() method to copy elements present in the stream into specified array
Integer[] ir =
l1.stream().toArray(Integer[] :: new); for(Integer i:
ir) {
sop(i);
}
VII.
Stream.of()method
we can also
apply a stream for group of values and for arrays.
Ex:
Stream s=Stream.of(99,999,9999,99999); s.forEach(System.out:: println);
Double[] d={10.0,10.1,10.2,10.3};
Stream s1=Stream.of(d);
s1.forEach(System.out :: println);
1 Comments
Your blog got me to learn a lot, thanks for sharing, nice article
ReplyDelete