java8 training - class 3

Post on 17-Jan-2017

93 Views

Category:

Software

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

http://www.marutsingh.com

Java8 Training – Class 3

-Marut SinghEmail: singh.marut@gmail.com

https://github.com/singhmarut/java8traininghttp://www.Marutsingh.com

http://www.marutsingh.com

Stream Operations Intermediate

Lazy Always create a new stream Stateless (filter, map) Stateful (distinct,sorted)

Terminal

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();

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);

http://www.marutsingh.com

Statistics

Arrays.asList(3, 2, 2, 3, 7, 3, 5) .stream().mapToInt((x) -> x).getMax();

http://www.marutsingh.com

Optional Class

Avoid NullPointerException Optional.empty() Optional.of(Class<T>) Optional.ofNullable(Class<T>)

http://www.marutsingh.com

Lazy Processing

By default all intermediate operations are lazy They all return Stream<T>

http://www.marutsingh.com

Sorting a stream

new Random().ints().limit(10).sorted().forEach(rand -> System.out.println(rand));

http://www.marutsingh.com

Stream operations

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.

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

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.

http://www.marutsingh.com

Java.time

ZoneId ireland = ZoneId.ofOffset("GMT", ZoneOffset.ofHours(+3));

http://www.marutsingh.com

File NIO

Main Differences Betwen Java NIO and IO

IO NIOStream oriented Buffer orientedBlocking IO Non blocking IO  Selectors

top related