What is Stream?
Stream represents a sequence of objects from a source, which supports
aggregate operations. Following are the characteristics of a Stream −
·
Sequence of elements − A stream provides a set of elements of specific type in a
sequential manner. A stream gets/computes element on demand. It never stores
the elements.
·
Source − Stream takes
Collections, Arrays, or I/O resources as input source.
·
Aggregate operations − Stream supports aggregate operations like filter, map, limit,
reduce, find, match, and so on.
·
Pipelining − Most of the stream operations return stream itself so that their
result can be pipelined. These operations are called intermediate operations
and their function is to take input, process them, and return output to the
target. collect () method is a terminal operation which is normally present at
the end of the pipelining operation to mark the end of the stream.
·
Automatic iterations − Stream operations do the iterations internally over the source
elements provided, in contrast to Collections where explicit iteration is
required.
Generating Streams
With
Java 8, Collection interface has two methods to generate a Stream.
·
stream () − Returns a sequential stream considering
collection as its source.
·
parallelStream() − Returns a parallel Stream considering
collection as its source.
forEach
Stream has provided a new method ‘forEach’ to iterate each element
of the stream. The following code segment shows how to print 10 random numbers
using forEach
map
The ‘map’ method is used to map each element to its corresponding
result. The following code segment prints unique squares of numbers using map.
filter
The ‘filter’ method is used to eliminate elements based on a criteria.
The following code segment prints a count of empty strings using filter.
limit
The ‘limit’ method is used to reduce the size of the stream. The
following code segment shows how to print 10 random numbers using limit.
sorted
The ‘sorted’ method is used to sort
the stream. The following code segment shows how to print 10 random numbers in
a sorted order.
Parallel Processing
parallelStream is the alternative of stream for parallel
processing. Take a look at the following code segment that prints a count of
empty strings using parallelStream
Collectors
Collectors are used to combine the result of processing on the
elements of a stream. Collectors can be used to return a list or a string.
1)Example of forEach and Filter
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ExampleOfForEachAndFilter
{
public static void main(String []arg)
{
//forEach
Example
Stream<Integer> stream = Stream.of(1,2,3,4,5,6,7,8,9);
stream.forEach(p ->
System.out.println("stream::"+p));
Stream<Integer> stream1 = Stream.of( new Integer[]{1,2,3,4,5,6,7,8,9} );
stream1.forEach(p ->
System.out.println("stream1::"+p));
List<Integer> list = new ArrayList<Integer>();
for(int i = 1; i< 10; i++){
list.add(i);
}
Stream<Integer> stream2 = list.stream();
stream2.forEach(p ->
System.out.println("stream2::"+p));
List<Integer> list2 = new ArrayList<Integer>();
for(int i = 1; i< 10; i++){
list2.add(i);
}
//forEach
Example and Filter Example
Stream<Integer> stream3 = list2.stream();
List<Integer> evenNumbersList = stream3.filter(i ->i%2 == 0).collect(Collectors.toList());
System.out.print("stream3::"+evenNumbersList);
}
}
2)Example of map collector
public class Persion
{
private String name;
private int age;
public Persion(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class personCollector {
public List<Persion> getDetails() {
List<Persion> details = new ArrayList<Persion>();
details.add(new Persion("Vipendra Kumar", 30));
details.add(new Persion("Rohit Kumar", 30));
details.add(new Persion("Swapnil Shinde", 30));
details.add(new Persion("Narendra Tripathi", 30));
details.add(new Persion(null, 0));
return details;
}
public static void main(String arg[]) {
List<Persion> data = new Dao().getDetails();
//
data.forEach(p->System.out.println(p));
List<String> collect = data.stream().filter(p -> p.getName()
!= null).map(Persion::toString)
.collect(Collectors.toList());
collect.forEach(System.out::println);
}
}
3) Example
of Limit
Public class limitExample
{
Public static void main(String arg[])
{
Random random = new Random();
random.ints().limit(10).forEach(System.out::println);
}}
4) Example
of sort
No comments:
Post a Comment