Stream Api in java 8
--------------------------------------------------------------------------------------------------------------------------
collect() method example in java 8
package streamconcept;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class CollectMethodExample {
public static void main(String[] args) {
ArrayList<String> l=new ArrayList<>();
l.add("govind");
l.add("ballabh");
l.add("khan");
l.add("java");
l.add("technology");
System.out.println(l);
List<String> l1 = l.stream().filter(s->s.length()>=5).collect(Collectors.toList());
System.out.println(l1);
List<String> l2 = l.stream().map(s->s.toUpperCase()).collect(Collectors.toList());
System.out.println(l2);
}
}
o/p:-----------------------------------------------------
[govind, ballabh, khan, java, technology]
[govind, ballabh, technology]
[GOVIND, BALLABH, KHAN, JAVA, TECHNOLOGY]
================================================================================================================================
//count() method example.....................
package streamconcept;
import java.util.ArrayList;
public class CountMethodExample {
public static void main(String[] args) {
ArrayList<String> l=new ArrayList<>();
l.add("govind");
l.add("ballabh");
l.add("khan");
l.add("java");
l.add("technology");
System.out.println(l);
long count = l.stream().filter(s->s.length()>=5).count();
System.out.println("The Length of strings whose length>=5="+count);
}
}
o/p:-----------------------------------------------
[govind, ballabh, khan, java, technology]
The Length of strings whose length>=5=3
=====================================================================================================================================
Sorted()
Sorted(comparator c) example in java:---------------------------
package streamconcept;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class SortedMethodExample {
public static void main(String[] args) {
ArrayList<Integer> l=new ArrayList<>();
l.add(0);
l.add(10);
l.add(20);
l.add(5);
l.add(15);
l.add(25);
System.out.println(l);
List<Integer> l1 = l.stream().sorted().collect(Collectors.toList());
System.out.println(l1);
List<Integer> l2 = l.stream().sorted((i1,i2)->-i1.compareTo(i2)).collect(Collectors.toList());
System.out.println(l2);
}
}
o/p:-----------------------------------------------
[0, 10, 20, 5, 15, 25]
[0, 5, 10, 15, 20, 25]
[25, 20, 15, 10, 5, 0]
=====================================================================================================================================
foreach() method in java 8 ........................
package streamconcept;
import java.util.ArrayList;
public class ForEachMtehodExample {
public static void main(String[] args) {
ArrayList<String> l=new ArrayList<>();
l.add("govind");
l.add("ballabh");
l.add("khan");
l.add("java");
l.add("technology");
l.forEach(s->System.out.println(s));
}
}
o/p:----------------------
govind
ballabh
khan
java
technology
========================================================================================================================================
min( )
max( ) method in java 8:---------------------------------
package streamconcept;
import java.util.ArrayList;
public class MinMaxMethodExample {
public static void main(String[] args) {
ArrayList<Integer> l=new ArrayList<>();
l.add(0);
l.add(10);
l.add(20);
l.add(5);
l.add(15);
l.add(25);
System.out.println(l);
Integer min = l.stream().min((i1,i2)->i1.compareTo(i2)).get();
System.out.println("Minimum value="+min);
Integer max = l.stream().max((i1,i2)->i1.compareTo(i2)).get();
System.out.println("Maximum Value="+max);
}
}
o/p:--------------------------------------------
[0, 10, 20, 5, 15, 25]
Minimum value=0
Maximum Value=25
======================================================================================================================================
//toArray() method example in java8
package streamconcept;
import java.util.ArrayList;
public class ToArrayMethodExample {
public static void main(String[] args) {
ArrayList<Integer> l=new ArrayList<>();
l.add(0);
l.add(10);
l.add(20);
l.add(5);
l.add(15);
l.add(25);
System.out.println(l);
Integer[] array = l.stream().toArray(Integer []::new);
for(Integer x:array)
{
System.out.println(x);
}
}
}
o/p:-----------------------
[0, 10, 20, 5, 15, 25]
0
10
20
5
15
25
========================================================================================================================================
//Stream.of() method in java 8
package streamconcept;
import java.util.stream.Stream;
public class StreamDotOfMethodExample {
public static void main(String[] args) {
Stream<Integer> s=Stream.of(9,99,999,9999,99999);
s.forEach(System.out::println);
Double d[]= {10.0,10.1,10.2,10.3,10.4,10.5};
Stream<Double> s1=Stream.of(d);
s1.forEach(System.out::println);
}
}
o/p:------------------------------------
9
99
999
9999
99999
10.0
10.1
10.2
10.3
10.4
10.5
--------------------------------------------------------------------------------------------------------------------------
collect() method example in java 8
package streamconcept;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class CollectMethodExample {
public static void main(String[] args) {
ArrayList<String> l=new ArrayList<>();
l.add("govind");
l.add("ballabh");
l.add("khan");
l.add("java");
l.add("technology");
System.out.println(l);
List<String> l1 = l.stream().filter(s->s.length()>=5).collect(Collectors.toList());
System.out.println(l1);
List<String> l2 = l.stream().map(s->s.toUpperCase()).collect(Collectors.toList());
System.out.println(l2);
}
}
o/p:-----------------------------------------------------
[govind, ballabh, khan, java, technology]
[govind, ballabh, technology]
[GOVIND, BALLABH, KHAN, JAVA, TECHNOLOGY]
================================================================================================================================
//count() method example.....................
package streamconcept;
import java.util.ArrayList;
public class CountMethodExample {
public static void main(String[] args) {
ArrayList<String> l=new ArrayList<>();
l.add("govind");
l.add("ballabh");
l.add("khan");
l.add("java");
l.add("technology");
System.out.println(l);
long count = l.stream().filter(s->s.length()>=5).count();
System.out.println("The Length of strings whose length>=5="+count);
}
}
o/p:-----------------------------------------------
[govind, ballabh, khan, java, technology]
The Length of strings whose length>=5=3
=====================================================================================================================================
Sorted()
Sorted(comparator c) example in java:---------------------------
package streamconcept;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class SortedMethodExample {
public static void main(String[] args) {
ArrayList<Integer> l=new ArrayList<>();
l.add(0);
l.add(10);
l.add(20);
l.add(5);
l.add(15);
l.add(25);
System.out.println(l);
List<Integer> l1 = l.stream().sorted().collect(Collectors.toList());
System.out.println(l1);
List<Integer> l2 = l.stream().sorted((i1,i2)->-i1.compareTo(i2)).collect(Collectors.toList());
System.out.println(l2);
}
}
o/p:-----------------------------------------------
[0, 10, 20, 5, 15, 25]
[0, 5, 10, 15, 20, 25]
[25, 20, 15, 10, 5, 0]
=====================================================================================================================================
foreach() method in java 8 ........................
package streamconcept;
import java.util.ArrayList;
public class ForEachMtehodExample {
public static void main(String[] args) {
ArrayList<String> l=new ArrayList<>();
l.add("govind");
l.add("ballabh");
l.add("khan");
l.add("java");
l.add("technology");
l.forEach(s->System.out.println(s));
}
}
o/p:----------------------
govind
ballabh
khan
java
technology
========================================================================================================================================
min( )
max( ) method in java 8:---------------------------------
package streamconcept;
import java.util.ArrayList;
public class MinMaxMethodExample {
public static void main(String[] args) {
ArrayList<Integer> l=new ArrayList<>();
l.add(0);
l.add(10);
l.add(20);
l.add(5);
l.add(15);
l.add(25);
System.out.println(l);
Integer min = l.stream().min((i1,i2)->i1.compareTo(i2)).get();
System.out.println("Minimum value="+min);
Integer max = l.stream().max((i1,i2)->i1.compareTo(i2)).get();
System.out.println("Maximum Value="+max);
}
}
o/p:--------------------------------------------
[0, 10, 20, 5, 15, 25]
Minimum value=0
Maximum Value=25
======================================================================================================================================
//toArray() method example in java8
package streamconcept;
import java.util.ArrayList;
public class ToArrayMethodExample {
public static void main(String[] args) {
ArrayList<Integer> l=new ArrayList<>();
l.add(0);
l.add(10);
l.add(20);
l.add(5);
l.add(15);
l.add(25);
System.out.println(l);
Integer[] array = l.stream().toArray(Integer []::new);
for(Integer x:array)
{
System.out.println(x);
}
}
}
o/p:-----------------------
[0, 10, 20, 5, 15, 25]
0
10
20
5
15
25
========================================================================================================================================
//Stream.of() method in java 8
package streamconcept;
import java.util.stream.Stream;
public class StreamDotOfMethodExample {
public static void main(String[] args) {
Stream<Integer> s=Stream.of(9,99,999,9999,99999);
s.forEach(System.out::println);
Double d[]= {10.0,10.1,10.2,10.3,10.4,10.5};
Stream<Double> s1=Stream.of(d);
s1.forEach(System.out::println);
}
}
o/p:------------------------------------
9
99
999
9999
99999
10.0
10.1
10.2
10.3
10.4
10.5
No comments:
Post a Comment