What is the differences between java.util streams and java.io streams?
java.util streams meant for processing objects from the collection. i.e., 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
1. 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
here (Predicate
Ex:
Stream s=c.stream(); Stream s1=s.filter(i –> i%2==0);
Hence to filter elements of collection based on some boolean condition 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
import java.util.*; class Test { public static void main(String[] args) { ArrayList<Integer> I1 = new ArrayList<Integer>(); for(int i=0; i<=10; i++) { I1.add(i); } System.out.println(I1); ArrayList<Integer> I2 = new ArrayList<Integer>(); for(Integer i:I1) { if(i%2 == 0) I2.add(i); } System.out.println(I2); } }
Approach-2: With Streams
import java.util.*; import java.util.stream.*; class Test { public static void main(String[] args) { ArrayList<Integer> I1 = new ArrayList<Integer>(); for(int i=0; i<=10; i++) { I1.add(i); } System.out.println(I1); List<Integer> I2 = I1.stream().filter(I –> i%2==0).collect(Collectors.toList()); System.out.println(I2); } }
Ex:
Program for map() and collect() Method
import java.util.*; import java.util.stream.*; class Test { public static void main(String[] args) { ArrayList<String> I = new ArrayList<String>(); I.add(“rvk”); I.add(“rk”); I.add(“rkv”); I.add(“rkvi”); I.add(“rvkir”); System.out.println(I); List<String> I2 = I.Stream().map(s –> s.toUpperCase()).collect(Collectors.toList()); System.out.println(I2); } }
Processing by count() method
This method returns number of elements present in the stream.
public long count()
Ex:
long count=I.stream().filters(s –> s.length()==5).count(); sop(“the number of 5 length strings is:”+count);
Processing by sorted() method
If we sort the elements present inside stream then we should go for sorted() method. The sorted 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> I3=I.stream().sorted().collect(Collectors.toList()); sop(“according to default natural sorting:”+I3); List<String> I4=I.stream().sorted((s1, s2) –> -s1.compareTo(s2)).collect(Collectors.toList()); sop(“according to customized sorting order:”+I4);
Processing by min() and max() methods s1.compareTo(s2)).get();
sop(“minimum value is:”+min);
String max=I.stream().max((s1, s2) –> s1.compareTo(s2)).get();
sop(“maximum value is:”+max);
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:
I.stream().forEach(s–>sop(s));
I3.stream.forEach(System.out:: println);
Ex:
import java.util.*; import java.util.stream.*; class Test { public static void main(String[] args) { ArrayList<Integer> I1 = new ArrayList<Integer>(); I1.add(0); I1.add(15); I1.add(10); I1.add(5); I1.add(30); I1.add(25); I1.add(20); System.out.println(I1); ArrayList<Integer> I2 = I1.stream().map(i–> i+10).collect(Collectors.toList()); System.out.println(I2); long count = I1.stream().filter(i–>i%2==0).count(); System.out.println(count); List<Integer> I3=I1.stream().sorted().collect(Collectors.toList()); System.out.println(I3); Comparator<Integer> comp=(i1,i2)–>i1.compareTo(i2); List<Integer> I4=I1.stream().sorted(comp).collect(Collectors.toList()); System.out.println(I4); Integer min=I1.stream().min(comp).get(); System.out.println(min); Integer max=I1.stream().max(comp).get(); System.out.println(max); I3.stream().forEach(i–>sop(i)); I3.stream().forEach(System.out:: println); } }
toArray() method
We can use toArray() method to copy elements present in the stream into specified array.
Integer[] ir = I1.stream().toArray(Integer[] :: new); for(Integer i: ir) { sop(i); }
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);
Thanks for reading. If you like this post please follow us for more updates about technology related updates.
No comments:
Post a Comment