JAVA Features – Stream API – Part 2

This blog explains about Stream API in java is given below : 

Print Array values in Sorted Order:

int[] ar = {10,5,7,3,6}; 
		Arrays
		.stream(ar) //Intermediate Operation
		.sorted()   // Intermediate Operation
		.forEach(System.out::println); // Terminal Operation

Note:

Intermediate Operations are Lazy Operations
Terminal Operations are Eager Operations

Find average of elements in an Array:

int[] ar = {10,5,7,3,10}; 
		OptionalDouble od = Arrays
		.stream(ar) //Intermediate Operation
		.average();// Intermediate Operation
		// Terminal Operation
		System.out.println(od.getAsDouble()); 

Finding max of given Array:

int[] ar = {10,5,7,3,12}; 
		OptionalInt od = Arrays
						.stream(ar) 
						.max(); 
		// Terminal Operation
		System.out.println(od.getAsInt()); 

Finding minimum of given Array:

int[] ar = {10,5,7,3,12}; 
OptionalInt od = Arrays
				.stream(ar) 
				.min(); 
System.out.println(od.getAsInt()); 

Finding First value of given Array:

int[] ar = {10,5,7,3,12}; 
		OptionalInt od = Arrays
						.stream(ar) 
						.findFirst();
		System.out.println(od.getAsInt()); 

Finding any value of given Array:

int[] ar = {10,5,7,3,12}; 
		OptionalInt od = Arrays
						.stream(ar) 
						.findAny();
		System.out.println(od.getAsInt());

Finding count of elements of given Array:

	int[] ar = {10,5,7,3,12,5,4,3,2}; 
		long od = Arrays
						.stream(ar) 
						.count();
		System.out.println(od); 

Find Unique elements of given Array:

int[] ar = {10,5,7,3,12,5,4,3,2}; 
Arrays
.stream(ar) 
.distinct()
//.forEach(elem -> System.out.println(elem));
.forEach(System.out::println);

Find Multiples of 4 of given Array:

int[] ar = {10,5,7,3,12,5,4,3,2}; 
Arrays
.stream(ar)
.filter(no -> no%4==0)
.forEach(System.out::println);

Stream API with Collection:

Count of elements in a List

List<Integer> l = new ArrayList<Integer>(); 

l.add(10); 
l.add(20);
l.add(30);
l.add(45);

System.out.println(l
.stream()
.count()); 

Unique elements from ArrayList:

List<Integer> l = new ArrayList<Integer>(); 

l.add(10); 
l.add(20);
l.add(30);
l.add(45);
l.add(45);
l.add(20);

l
.stream()
.distinct()
.forEach(System.out::println);

Sort elements in an ArrayList:

List<Integer> l = new ArrayList<Integer>(); 

l.add(10); 
l.add(20);
l.add(30);
l.add(45);
l.add(45);
l.add(20);

l
.stream() //stream
.distinct() //10,30,20,45
.sorted() //10 20 30 45
.forEach(System.out::println);

Comparator Interface:

//Ascending / any order 
List<String> l = new ArrayList<String>(); 

l.add("abcdefgh"); 
l.add("bcde");
l.add("efghij");
l.add("cdefghijklmn");
l.add("wxyz");
l.add("pqrstu");

l
.stream() //stream
.distinct() //10,30,20,45
.sorted((elem1,elem2)-> elem2.compareTo(elem1)) //10 20 30 45
.forEach(System.out::println);

Change all elements of List to upper case:

List<String> l = new ArrayList<String>(); 

l.add("abcdefgh"); 
l.add("bcde");
l.add("efghij");
l.add("cdefghijklmn");
l.add("wxyz");
l.add("pqrstu");

l
.stream() //stream of Strings
.map(word -> word.toUpperCase()) //
.forEach(System.out::println);