Difference Between Parallel Stream and CompletableFuture in Java

I am going to discuss here CompletableFuture vs Parallel Stream in Java programming language. CompletableFuture extends Future with added advantage to allow the tasks finish in an ad hoc manner. In Parallel Stream task is divided into sub-tasks and run on separate threads to be completed faster. Both CompletableFuture and Parallel Stream were added in Java 8. I am not going to explain in details about CompletableFuture and Parallel Stream but you can always check the corresponding link to read details. But I am going to tell you in which…

Continue

Check a Given Date is Past, Today or Future’s Date in Java 8

In this example I am going to show you how to check if an input date is past date or today’s date or future date. I am using Java version 12 and for this example you should use at least Java version 8. Java 8 or higher version provides thread safe version of date time API that can be used safely in multi-threaded environment. If you want to calculate the future or past date by adding or removing days to the input or given date then you can read the…

Continue

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 I 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 you 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