Streams Api
Streams Api
Colletion vs Stream:
If we want to represent a group of objects as a single entity then it is represented as
collections
But if we want to process objects from the collection then we should go for streams.
Syntax:
Stream s=collection.Stream();
On the collection we are calling a stream method at the same time we
are storing it as stream method
EXAMPLE:
public class OwnDemo {
public static void main(String[] args) {
//create a stream from sources
Collection<String>collection=Arrays.asList("java","programming");
Stream<String> stream1=collection.stream(); //syntax of stream
stream1.forEach(System.out::println);
}
}
Here we used (System.out::println); and forEach in streams
Creating a stream of an array
public class OwnDemo {
public static void main(String[] args) {
Stream<String>stream=Stream.of("a","b","c");
stream.forEach(System.out::println);
}
}
Output:
a
b By using list and set , you can create streams
it will save the memory a,d reduce the size of the code
c
WITHOUT STREAM
public static void main(String[] args)
{
Set hashset= new HashSet();
hashset.add("aman");
hashset.add("akshatha");
hashset.add("arman");
hashset.add("arman");
hashset.add("aayush");
System.out.println(hashset);
Iterator i=hashset.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
WITH STREAM
• List<String>list=Arrays.asList("welcome","to","my","organization");
• Stream<String> stream2=list.stream(); // prints like a list
• stream2.forEach(System.out::println); //in sequential order
• List<String>list=Arrays.asList("welcome","to","my","organization");
• Set<String>set=new HashSet<>(list); //prints like a hashset
• Stream<String> stream3=set.stream(); elements are in random order
• stream3.forEach(System.out::println);
ForEach()
Method iterates through every element in the stream
Without stream:
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
for (Integer element : list) {
System.out.print(element + " ");}
With stream:
List list1 = Arrays.asList(1,3,5,7);
//list1.stream().forEach((k) -> {System.out.print(k + " ");});
list1.stream().forEach(System.out::println);
AGGREGATE OPERATIONS
It is a higher order function that applies a behaviour on elements in
stream
------filter(),map(),reduce(),limit(),find(),match() e.t.c
There are two types in aggregate operations
1.Intermediate operations
a.stateful-store information from prior invocation
distinct(),skip(),sorted()
b.stateless-do not store information from any prior invocation stack
filter(),map(),flatMap(),limit(),takeWhile(),dropWhile()
FILTER()
Used to select elements according to the predicate parameter passed
Syntax:Stream<T> filter(Predicate<? super T> predicate)
Map()
Map method maps the elements in the collection to other objects
according to the predicate based as parameter
Example:
List<Integer> list1 = Arrays.asList(11,22,44,21);
List<Integer> answer = list1.stream().map(x x*x).collect (Collectors.toList());
System.out.println(answer);
output:
sum:206
Min(),Max(),Sum(),Count()
MIN()--------gives minimum data in the list as result
MAX()--------gives maximum data in the list as result
SUM()--------gives sum of the data in the list as output
COUNT()------- gives number of data in the list as output
• //calculate total amount of food
double SumOfPrice=foodlist.stream().collect(Collectors.summingDouble(id-
>id.price));
System.out.println( SumOfPrice);
//calculating count
double countPrice=foodlist.stream().filter(a->a.price>120).count();
System.out.println(countPrice);
LIMIT()
Stream<T> limit(long N) //syntax
Where N is the number of elements the stream should be limited to and this function returns new stream as output
EXAMPLE:
Optional<String> op2
= Optional.ofNullable(null);
// print value
System.out.println("Optional 2: "+ op2);because we given null