STREAMSTo process objects of the collection, in 1.8 version Streams concept introduced.What is the differences between java.util.stream and java.io streams ?
What is the difference between collection and stream ?
1) Configuration:We can configure either by using filter mechanism or by using map mechanism.Filtering:
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 Boolean condition we should go for filter() method. Mapping:
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
I. Processing by collect() methodThis 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 1) import java.util.*; 2) class Test { 3) public static void main(String[] args) { 4) ArrayList<Integer> l1 = new ArrayList<Integer>(); 5) for(int i=0; i<=10; i++) { 6) l1.add(i); 7) } 8) System.out.println(l1); 9) ArrayList<Integer> l2 = new ArrayList<Integer>(); 10) for(Integer i:l1) { 11) if(i%2 == 0) 12) l2.add(i); 13) } 14) System.out.println(l2); 15) } 16) }Approach-2: With Streams 1) import java.util.*; 2) import java.util.stream.*; 3) class Test { 4) public static void main(String[] args) { 5) ArrayList<Integer> l1 = new ArrayList<Integer>(); 6) for(inti=0; i<=10; i++) { 7) l1.add(i); 8) } 9) System.out.println(l1); 10) List<Integer> l2 = l1.stream().filter(i -> i%2==0).collect(Collectors.toList()); 11) System.out.println(l2); 12) } 13) }Ex: Program for map() and collect() Method 1) import java.util.*; 2) import java.util.stream.*; 3) class Test { 4) public static void main(String[] args) { 5) ArrayList<String> l = new ArrayList<String>(); 6) l.add("rvk"); l.add("rk"); l.add("rkv"); l.add("rvki"); l.add("rvkir"); 7) System.out.println(l); 8) List<String> l2 = l.stream().map(s ->s.toUpperCase()).collect(Collectors.toList()); 9) System.out.println(l2); 10) } 11) } II. Processing by count() methodThis 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); III. Processing by sorted() method
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); IV. Processing by min() and max() methodsmin(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); V. forEach() method
Ex: l.stream().forEach(s->sop(s)); l3.stream().forEach(System.out:: println); Ex: 1) import java.util.*; 2) import java.util.stream.*; 3) class Test1 { 4) public static void main(String[] args) { 5) ArrayList<Integer> l1 = new ArrayaList<Integer>(); 6) l1.add(0); l1.add(15); l1.add(10); l1.add(5); l1.add(30); l1.add(25); l1.add(20); 7) System.out.println(l1); 8) ArrayList<Integer> l2=l1.stream().map(i-> i+10).collect(Collectors.toList()); 9) System.out.println(l2); 10) long count = l1.stream().filter(i->i%2==0).count(); 11) System.out.println(count); 12) List<Integer> l3=l1.stream().sorted().collect(Collectors.toList()); 13) System.out.println(l3); 14) Comparator<Integer> comp=(i1,i2)->i1.compareTo(i2); 15) List<Integer> l4=l1.stream().sorted(comp).collect(Collectors.toList()); 16) System.out.println(l4); 17) Integer min=l1.stream().min(comp).get(); 18) System.out.println(min); 19) Integer max=l1.stream().max(comp).get(); 20) System.out.println(max); 21) l3.stream().forEach(i->sop(i)); 22) l3.stream().forEach(System.out:: println); 23) 24) } 25) } VI. toArray() methodWe can use toArray() method to copy elements present in the stream into specified arrayInteger[] ir = l1.stream().toArray(Integer[] :: new); for(Integer i: ir) { sop(i); } VII. Stream.of()methodWe 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); |