Posts

Showing posts with the label Java 9

All advanced sorting techniques using Java 8 and Streams

package com.dpq.interview.Q; import java.math.BigInteger; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.List; public class SortingDemo { public static void main(String[] args) throws ParseException { allSortingTechniquesAfterJava8(); } public static void allSortingTechniquesAfterJava8() throws ParseException { List<Employee> list = populateList(); System.out.println(“####################################### Natiral soring by Employee Id ###############################################”); list.stream().sorted().forEach(e -> System.out.println(e)); System.out.println(“####################################### Natiral soring by Employee Id but in desending order ###############################################”); list.stream().sorted(Collections.reverseOrder()).forEach(e -> System.out.println(e)); List<Employee> sortedList = list

Java 9 Stream API Improvements

Image
Post Brief Table of Content: Introduction Java SE 8: Stream API Basics Java SE 9: Stream API Improvements Java SE 9: Stream API takeWhile() Method Java SE 9: Stream API dropWhile() Method Java SE 9: Stream API iterate() Method Java SE 9: Stream API ofNullable() Method Introduction Oracle Corporation is going to release Java New Version: Java SE 9 around End of March 2017. So, I would like to deliver a series of Posts on Java SE 9 New Features. It’s my ninth post in this series. In this post, first we will discuss about Java SE 8 Stream API basics, then will introduce Java SE 9 Stream API Improvements. Let us start with Stream API basics in next section. Java SE 8: Stream API Basics As we know, one of the big changes Oracle Corp has introduced in Java SE 8 release is  Stream API . The other big change is  Lambda Expressions . What is a Stream? A Stream is a sequence of elements and supports a set of aggregate operations on them easily. It supports those operations either in sequential o

Java 9 Try-With-Resources Improvements

Post Brief Table of Content: Introduction Java SE 7: Try-With-Resources Basics Java SE 7: Try-With-Resources Rules Java SE 9: Try-With-Resources Improvements Introduction Oracle Corporation is going to release Java New Version: Java SE 9 around End of March 2017. So, I would like to deliver a series of Posts on Java SE 9 New Features. It’s my eighth post in this series. In this post, we are going to discuss about some Improvements to Try-With-Resources in Java SE 9. Let us start exploring that construct now. Java SE 7 has introduced a new construct: Try-With-Resources Statement for better Exception Handling. Without this construct, Developer has to write lot of redundant and ugly code. If Developer forgets about closing resources properly, we will get Resource Leakage issues in our application. The main goals of this new feature is : Syntactic Sugar Better Readable Code No need to do null checks Better Resource Management To avoid memory leakages Try-With-Resources Example-1:- void tes

Java 9 Reactive Streams

Image
Reactive Streams is about asynchronous processing of stream, so there should be a  Publisher  and a  Subscriber . The Publisher publishes the stream of data and the Subscriber consumes the data. Sometimes we have to transform the data between Publisher and Subscriber.  Processor  is the entity sitting between the end publisher and subscriber to transform the data received from publisher so that subscriber can understand it. We can have a chain of processors. It’s very clear from the above image that Processor works both as Subscriber and a Publisher. Java 9 Flow API Java 9 Flow API implements the  Reactive Streams Specification . Flow API is a combination of  Iterator  and  Observer  pattern. Iterator works on pull model where application pulls items from the source, whereas Observer works on push model and reacts when item is pushed from source to application. Java 9 Flow API subscriber can request for N items while subscribing to the publisher. Then the items are pushed from publishe

Java 9 Private methods in Interfaces

Java 9 onwards, you can include private methods in interfaces. Before Java 9 it was not possible. Interfaces till Java 7 In Java SE 7 or earlier versions, an interface can have only two things i.e.  Constant variables and Abstract methods . These interface methods MUST be implemented by classes which choose to implement the interface. // Java 7 program to illustrate // private methods in interfaces public interface TempI { public abstract void method(int n); } class Temp implements TempI { @Override public void method(int n) { if (n % 2 == 0) System.out.println(“deepakbhardwaj”); else System.out.println(“DEEPAKBHARDWAJ”); } public static void main(String[] args) { TempI in1 = new Temp(); TempI in2 = new Temp(); in1.method(4); in2.method(3); } } OUTPUT : deepakbhardwaj DEEPAKBHARDWAJ Java 8 Interface Changes Some new features to interface were introduced in Java 8 i.e. Default methods and Static methods feature. In Java 8, an interface can have only four types: Constant variable

Java 9 changes for Collections

n this post, we are going to discuss one more Java SE 9 New Feature:  “Factory Methods for Immutable List, Set and Map”  with some simple and suitable examples. Java SE 8: Empty Immutable List, Set and Map In Java SE 8 and earlier Versions, if we want to create an empty Immutable or Unmodifiable List, we should use Collections class utility methods:  unmodifiableList , unmodifiableSet and unmodifiableMap as shown below: Example:- List<String> emptyList = new ArrayList<>(); List<String> immutableList = Collections.unmodifiableList(emptyList); Set<String> emptySet = new HashSet<>(); Set<String> immutableSet = Collections.unmodifiableSet(emptySet); Map<Integer,String> emptyMap = new HashMap<>(); Map<Integer,String> immutableEmptyMap = Collections.unmodifiableMap(emptyMap); NOTE:-  Diamond Operator does NOT work in Java SE 6 and earlier versions. Rest of the code is same for all Java versions. Here we can observe that to create an emp

Java 9 Features

Some of the important java 9 features are: Java 9 REPL (JShell) Factory Methods for Immutable List, Set, Map and Map.Entry Private methods in Interfaces Java 9 Module System Process API Improvements Try With Resources Improvement CompletableFuture API Improvements Reactive Streams Diamond Operator for Anonymous Inner Class Optional Class Improvements Stream API Improvements Enhanced @Deprecated annotation HTTP 2 Client Multi-Resolution Image API Miscellaneous Java 9 Features Java 9 REPL (JShell) Oracle Corp has introduced a new tool called “jshell”. It stands for Java Shell and also known as REPL (Read Evaluate Print Loop). It is used to execute and test any Java Constructs like class, interface, enum, object, statements etc. very easily. We can download JDK 9 EA (Early Access) software from https://jdk9.java.net/download/ G:>jshell | Welcome to JShell — Version 9-ea | For an introduction type: /help intro jshell> int a = 10 a ==> 10 jshell> System.out.println(“a value = ”