Java8 Training – Class 3
-Marut Singh
Email: singh.marut@gmail.com
https://github.com/singhmarut/java8training
http://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
 forEach
Random random = new Random();
random.ints().limit(10).forEach(System.out::println);
 map
List<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());
 filter
List<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();
 Collectors
List<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
http://www.marutsingh.com
IO NIO
Stream oriented Buffer oriented
Blocking IO Non blocking IO
Selectors

Java8 training - class 3

  • 1.
    Java8 Training –Class 3 -Marut Singh Email: singh.marut@gmail.com https://github.com/singhmarut/java8training http://www.Marutsingh.com http://www.marutsingh.com
  • 2.
    Stream Operations  Intermediate Lazy  Always create a new stream  Stateless (filter, map)  Stateful (distinct,sorted)  Terminal http://www.marutsingh.com
  • 3.
    Stream Operations  forEach Randomrandom = new Random(); random.ints().limit(10).forEach(System.out::println);  map List<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());  filter List<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
  • 4.
    Stream operations  sorted Randomrandom = 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();  Collectors List<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
  • 5.
    Statistics  Arrays.asList(3, 2,2, 3, 7, 3, 5) .stream().mapToInt((x) -> x).getMax(); http://www.marutsingh.com
  • 6.
    Optional Class  AvoidNullPointerException  Optional.empty()  Optional.of(Class<T>)  Optional.ofNullable(Class<T>) http://www.marutsingh.com
  • 7.
    Lazy Processing  Bydefault all intermediate operations are lazy  They all return Stream<T> http://www.marutsingh.com
  • 8.
    Sorting a stream new Random().ints().limit(10).sorted().forEach(rand -> System.out.println(rand)); http://www.marutsingh.com
  • 9.
  • 10.
    Parallel Streams  behavioralparameters 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
  • 11.
    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
  • 12.
    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
  • 13.
    Java.time  ZoneId ireland= ZoneId.ofOffset("GMT", ZoneOffset.ofHours(+3)); http://www.marutsingh.com
  • 14.
    File NIO  MainDifferences Betwen Java NIO and IO http://www.marutsingh.com IO NIO Stream oriented Buffer oriented Blocking IO Non blocking IO Selectors