Parallelism Example in Java Stream API

I am going to show you how to work with parallel stream in Java 8 or higher version of Java. In parallel computing a task is divided into sub-tasks and these sub-tasks are computed parallelly in each separate threads and finally the result of the solution for each sub-tasks is combined. One of the features for computing parallelism is to use Java’s join/fork framework, where you need to mention how the particular task will be sub-divided or partitioned into sub-tasks. The main difficulty for parallel computing in applications is the…

Continue

Add or subtract days, weeks, months, years on Java 8 Date

In this tutorial I am going to show you how to add days, weeks, months, years to a particular date in Java 8 or later. Similarly I will also show how to substract days, weeks, months, years from a particular date. The date is actually LocalDate API in Java 8 or later. The one of the important improvements in Java 8 date time API was introduced is thread safety. So date time API in Java 8 or later is thread safe and can be used in multi-threaded environment without applying…

Continue

How to find unique strings or objects using Java stream API

Introduction In this tutorial we will see an example on how to find unique strings or objects using Java stream API. Stream API was introduced in Java 8 and we are going to see how to use this stream API to remove duplicates from strings or objects. Java stream API has a method distinct() that removes duplicates and returns unique strings from a list of strings. But for objects we cannot directly apply this distinct() method directly to find out unique elements from a list of objects. Therefore we will…

Continue

Supplier and Consumer in Java 8

Introduction We will discuss here about the new feature added to Java 8 – a functional interface, Supplier and Consumer. In simple words, a supplier is a method that returns a value. A supplier is any method which takes no arguments and returns a value. Its job is to supply an instance of an expected class. Whereas, a consumer is a method that consumes some value (as in method argument), and does some operations on them. So a Consumer is any method which takes arguments and returns nothing. It is…

Continue

Convert List of Map Objects into List of Objects using Java Stream

Introduction Here I will show you how to convert List of Map objects into List of objects using Java 8’s or later’s Stream API. The List of Map objects can be found as a result of selecting multiple records from database table or any other sources. Then you may need to convert into List of objects to make it better readability for further using in the application code.

Continue

Convert Milliseconds into Years, Months, Weeks, Days, Hours, Minutes, Seconds in Java

Introduction This example shows you how to convert milliseconds into years, months, weeks, days, hours, minutes, seconds in Java. Sometimes we need to convert the milliseconds into years or months or weeks or days or hours or minutes or seconds. For example, when you want to check the exact time taken by a program to execute then you may need to calculate the time. So in this case you get the start time in milliseconds and end time in milliseconds and you want to check how much time that program…

Continue

Convert Milliseconds into Days, Hours, Minutes, Seconds in Java

Introduction This example shows you hot to convert milliseconds into days, hours, minutes, seconds in Java. Sometimes we need to convert the milliseconds into days or hours or minutes or seconds. Typically when you want to check the exact time taken to execute a program then you may need to calculate the time. So in this case you get the start time in milliseconds and end time in milliseconds and you want to check how much time that program took to execute. Therefore you would like to convert the milliseconds…

Continue

Java 8 default and static methods example

In this tutorial I am going to show how default and static methods can be included into Java 8 interface. Prior to Java 8 we did not have such mechanism to include default method or static method into interface but Java 8 provides such option to include those methods. Static methods are regarded as utility methods, not as essential, core methods. Whereas default methods are treated as common behaviors for all the client classes.

Continue

Custom Thread Pool in Java

Introduction In this tutorial I am going to show how to create custom thread pool in Java. Multi-threaded and multi-process programmings are great ways to optimize CPU usage and get things done quickly. What is thread pool? Thread pool is a collection of already created worker threads ready to perform certain tasks. Instead of creating and discarding thread once the task is done, thread pool reuses the thread in the form of worker thread. The worker thread differently exists from Runnable or Callable tasks it executes and often used to…

Continue