Posts

Showing posts with the label Java 8

Java | Shutdown Hooks for Java Applications

When java application is stopped, its used services/processes needs to be shutdown gracefully. Java shutdown hooks provides way to gracefully stop the services/processes. We can write our own logic to do things when shutdown hooks are called.  Shutdown hooks are called when -  1. When the main thread exits, the JVM starts its shutdown process 2. Sending an interrupt signal from the OS. Eg. Ctrl + C or OS Shutdown 3. Calling System.exit() from Java code Shutdown hooks are basically initialized but unstarted threads. Shutdown hooks are only called on above mentioned conditions, it is not called in case of jvm shut down abruptly eg. power failure etc.

Java 8 | Writing custom Stream API using builder pattern

In this post we are going to create simple Java 8   Stream API using builder pattern. Check builder pattern post here .  API will provide below four methods - sort - Sort the list limit - Fetch only N numbers from the list distinct - Remove duplicates from the list forEach - Print the list

Java 8 | Fetch top N unique numbers from list

Below Java 8 example shows how to fetch top N unique numbers from ArrayList. Traditional looping will require more logic to first remove duplicates, sort numbers and then store N numbers to different list. Java 8 provides stream API to do all the above steps in single line of code.  Descending order (N = 3)