Java 8 :: Streams and Lambda Expressions
/*
* Problem No: #2
* Problem Definition: How to Sort and Print List of Collection type Using Stream?
*
* Answer: By Using,
*>java.util.Collection.stream(): Returns a sequential Stream with this collection as its source.
* >java.util.stream.Stream.sorted(): Returns a stream consisting of the elements of this stream, sorted according to natural order.This is a stateful intermediate operation.
* >java.util.stream.Stream.forEach(): Performs an action for each element of this stream.This is a terminal operation.
*
* NOTE : In Case of Using java.util.stream.Stream.sorted() method for ordered streams, the sort is stable. For unordered streams, no stability guarantees are made.
*
* Program By: Mr.DIpak SOnar
* Date : 23rd Oct 2016
*/
import java.util.ArrayList;
import java.util.List;
public class StreamCollectionInitEx {
public static void main(String[] args) {
List<Integer> ListInt = new ArrayList<Integer>();
ListInt.add(21);
ListInt.add(111);
ListInt.add(189);
ListInt.add(3341);
ListInt.add(143);
ListInt.add(131);
ListInt.add(11);
ListInt.add(12);
ListInt.add(121);
ListInt.add(9919);
System.out.println("Sorting and Printing Stream Using java.util.stream.Stream.sorted() method::");
ListInt.stream().sorted().forEach(System.out :: println);
List<String> ListStr = new ArrayList<String>();
ListStr.add("Dipak");
ListStr.add("Amar");
ListStr.add("VishwaMitra");
ListStr.add("Naval");
ListStr.add("Alok");
ListStr.add("Virat");
ListStr.add("Pallav");
ListStr.add("Viral");
ListStr.add("Shail");
ListStr.add("Sonar");
System.out.println("Sorting & Printing Stream Using java.util.stream.Stream.sorted().forEach() method::");
ListStr.stream().sorted().forEach(System.out::println);
}
}
/*
* Problem No: #2
* Problem Definition: How to Sort and Print List of Collection type Using Stream?
*
* Answer: By Using,
*>java.util.Collection.stream(): Returns a sequential Stream with this collection as its source.
* >java.util.stream.Stream.sorted(): Returns a stream consisting of the elements of this stream, sorted according to natural order.This is a stateful intermediate operation.
* >java.util.stream.Stream.forEach(): Performs an action for each element of this stream.This is a terminal operation.
*
* NOTE : In Case of Using java.util.stream.Stream.sorted() method for ordered streams, the sort is stable. For unordered streams, no stability guarantees are made.
*
* Program By: Mr.DIpak SOnar
* Date : 23rd Oct 2016
*/
import java.util.ArrayList;
import java.util.List;
public class StreamCollectionInitEx {
public static void main(String[] args) {
List<Integer> ListInt = new ArrayList<Integer>();
ListInt.add(21);
ListInt.add(111);
ListInt.add(189);
ListInt.add(3341);
ListInt.add(143);
ListInt.add(131);
ListInt.add(11);
ListInt.add(12);
ListInt.add(121);
ListInt.add(9919);
System.out.println("Sorting and Printing Stream Using java.util.stream.Stream.sorted() method::");
ListInt.stream().sorted().forEach(System.out :: println);
List<String> ListStr = new ArrayList<String>();
ListStr.add("Dipak");
ListStr.add("Amar");
ListStr.add("VishwaMitra");
ListStr.add("Naval");
ListStr.add("Alok");
ListStr.add("Virat");
ListStr.add("Pallav");
ListStr.add("Viral");
ListStr.add("Shail");
ListStr.add("Sonar");
System.out.println("Sorting & Printing Stream Using java.util.stream.Stream.sorted().forEach() method::");
ListStr.stream().sorted().forEach(System.out::println);
}
}
No comments:
Post a Comment