Hi Guys,
We will have a look at powerful functionality in java8.

Stream vs Collection
Streams:
Takes advantage of declarative programming it gives hint about what operations need to perform on source.
Collection:
It has direct access on source (array, collections).
You will need to take care of iteration.
What is Stream?
Stream is composed of the Stream pipeline.
Stream=Source+Intermediary Operation+Terminal Operations
Source which may be array, collection.
Intermediary operations such as filter to collect results that satisfy some predicate.

Terminal operations:sum,avg,count
Computations are performed at this stage.
Facts about Streams
1.     Streams can’t be reused.
2.     No need to close the streams they are autoclosable.
3.     However the streams related to IOChannel(File) must be closed
Example on Streams
package com.seetest;
import java.util.ArrayList;
import java.util.List;
public class HelloWorld{
     public static void main(String []args){
        List<String> phones=newArrayList<String>();
        phones.add(“samsung”);
        phones.add(“xiomi”);
        phones.stream().filter(s->s.startsWith(“s”)).forEach(System.out::println);
        //String phone;
      
    
     }
}
            Methods in Stream Class:
   
1.     findAny
2.     findFirst
3.     limit
4.     min
5.     map()
6.     mapToInt()
7.     mapToDouble()
1.findAny:
Returns any element in the stream.
import java.util.ArrayList;
import java.util.List;
public class HelloWorld{
     public static void main(String []args){
        List<String> phones=newArrayList<String>();
        phones.add(“samsung”);
        phones.add(“xiomi”);
        phones.stream().findAny().ifPresent(System.out::println);
        //String phone;
      
    
     }
}
2.findFirst:
findFirst returns the first elements of the stream
import java.util.ArrayList;
import java.util.List;
public class HelloWorld{
     public static void main(String []args){
        List<String> phones=newArrayList<String>();
        phones.add(“samsung”);
        phones.add(“xiomi”);
        phones.stream().findFirst().ifPresent(System.out::println);
        //String phone;
      
    
     }
}
3.limit:

limits how many rows should be shown.
import java.util.ArrayList;
import java.util.List;
public class HelloWorld{
     public static void main(String []args){
        List<String> phones=newArrayList<String>();
        phones.add(“samsung”);
        phones.add(“xiomi”);
        phones.stream().limit(1).forEach(System.out::println);
        //String phone;
      
    
     }
}
4.min-
To find the minimum element in the system.
For this we need to implement the comparator
Signature: min() takes comparator
(s1,s2)->{return         s1.compareTo(s2);}
import java.util.ArrayList;
import java.util.List;
public class HelloWorld{
     public static void main(String []args){
        List<String> phones=newArrayList<String>();
        phones.add(“xiomi”);
        phones.add(“Samsung”);
        phones.stream().min((s1,s2)->{return         s1.compareTo(s2);}).ifPresent(s->System.out.println(s));
     }
 }

2 thoughts on “Stream() Java8”

  1. Processing streams lazily allows for significant efficiencies; in a pipeline such as the filter-map-sum example above, filtering, mapping, and summing can be fused into a single pass on the data, with minimal intermediate state.

Leave a Reply

Your email address will not be published. Required fields are marked *