Download - Java8 training - class 3
![Page 1: Java8 training - class 3](https://reader035.vdocument.in/reader035/viewer/2022062822/587dcf3e1a28ab5b5b8b51a7/html5/thumbnails/1.jpg)
http://www.marutsingh.com
Java8 Training – Class 3
-Marut SinghEmail: [email protected]
https://github.com/singhmarut/java8traininghttp://www.Marutsingh.com
![Page 2: Java8 training - class 3](https://reader035.vdocument.in/reader035/viewer/2022062822/587dcf3e1a28ab5b5b8b51a7/html5/thumbnails/2.jpg)
http://www.marutsingh.com
Stream Operations Intermediate
Lazy Always create a new stream Stateless (filter, map) Stateful (distinct,sorted)
Terminal
![Page 3: Java8 training - class 3](https://reader035.vdocument.in/reader035/viewer/2022062822/587dcf3e1a28ab5b5b8b51a7/html5/thumbnails/3.jpg)
http://www.marutsingh.com
Stream Operations
forEachRandom random = new Random(); random.ints().limit(10).forEach(System.out::println);
mapList<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5); //get list of unique squares List<Integer> squaresList = numbers.stream().map( i -> i*i).distinct().collect(Collectors.toList());
filterList<String>strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");//get count of empty string int count = strings.stream().filter(string -> string.isEmpty()).count();
![Page 4: Java8 training - class 3](https://reader035.vdocument.in/reader035/viewer/2022062822/587dcf3e1a28ab5b5b8b51a7/html5/thumbnails/4.jpg)
http://www.marutsingh.com
Stream operations sorted
Random random = new Random(); random.ints().limit(10).sorted().forEach(System.out::println);
Distinct Parallel Processing
List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl"); //get count of empty string int count = strings.parallelStream().filter(string -> string.isEmpty()).count();
CollectorsList<String>strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl"); List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList()); System.out.println("Filtered List: " + filtered); String mergedString = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining(", ")); System.out.println("Merged String: " + mergedString);
![Page 5: Java8 training - class 3](https://reader035.vdocument.in/reader035/viewer/2022062822/587dcf3e1a28ab5b5b8b51a7/html5/thumbnails/5.jpg)
http://www.marutsingh.com
Statistics
Arrays.asList(3, 2, 2, 3, 7, 3, 5) .stream().mapToInt((x) -> x).getMax();
![Page 6: Java8 training - class 3](https://reader035.vdocument.in/reader035/viewer/2022062822/587dcf3e1a28ab5b5b8b51a7/html5/thumbnails/6.jpg)
http://www.marutsingh.com
Optional Class
Avoid NullPointerException Optional.empty() Optional.of(Class<T>) Optional.ofNullable(Class<T>)
![Page 7: Java8 training - class 3](https://reader035.vdocument.in/reader035/viewer/2022062822/587dcf3e1a28ab5b5b8b51a7/html5/thumbnails/7.jpg)
http://www.marutsingh.com
Lazy Processing
By default all intermediate operations are lazy They all return Stream<T>
![Page 8: Java8 training - class 3](https://reader035.vdocument.in/reader035/viewer/2022062822/587dcf3e1a28ab5b5b8b51a7/html5/thumbnails/8.jpg)
http://www.marutsingh.com
Sorting a stream
new Random().ints().limit(10).sorted().forEach(rand -> System.out.println(rand));
![Page 9: Java8 training - class 3](https://reader035.vdocument.in/reader035/viewer/2022062822/587dcf3e1a28ab5b5b8b51a7/html5/thumbnails/9.jpg)
http://www.marutsingh.com
Stream operations
![Page 10: Java8 training - class 3](https://reader035.vdocument.in/reader035/viewer/2022062822/587dcf3e1a28ab5b5b8b51a7/html5/thumbnails/10.jpg)
http://www.marutsingh.com
Parallel Streams
behavioral parameters in stream pipelines whose source might not be concurrent should never modify the stream's data source.
A behavioral parameter is said to interfere with a non-concurrent data source if it modifies, or causes to be modified, the stream's data source.
![Page 11: Java8 training - class 3](https://reader035.vdocument.in/reader035/viewer/2022062822/587dcf3e1a28ab5b5b8b51a7/html5/thumbnails/11.jpg)
http://www.marutsingh.com
New DateTime API
Problems with old date time api (java.util.date) Not thread safe Inconsistent design
Default Date starts from 1900 (month starts from 1, and day starts from 0) Difficult time zone handling
![Page 12: Java8 training - class 3](https://reader035.vdocument.in/reader035/viewer/2022062822/587dcf3e1a28ab5b5b8b51a7/html5/thumbnails/12.jpg)
http://www.marutsingh.com
Java.time
Classes Local − Simplified date-time API with no complexity of timezone handling via
static methods LocalDate LocalTime LocalDateTime Instant (Instant is an instantaneous point on the global time-line (UTC), and is
unrelated to time-zone)
Zoned - Specialized date-time API to deal with various timezones. Periods - deal with date based amount of time. Duration - deal with time based amount of time.
![Page 13: Java8 training - class 3](https://reader035.vdocument.in/reader035/viewer/2022062822/587dcf3e1a28ab5b5b8b51a7/html5/thumbnails/13.jpg)
http://www.marutsingh.com
Java.time
ZoneId ireland = ZoneId.ofOffset("GMT", ZoneOffset.ofHours(+3));
![Page 14: Java8 training - class 3](https://reader035.vdocument.in/reader035/viewer/2022062822/587dcf3e1a28ab5b5b8b51a7/html5/thumbnails/14.jpg)
http://www.marutsingh.com
File NIO
Main Differences Betwen Java NIO and IO
IO NIOStream oriented Buffer orientedBlocking IO Non blocking IO Selectors