Find All Paths from Root to Leaf Nodes in Binary Tree using Java

In this example I am going to show you how to find all paths from root to leaf nodes in binary tree. So I am going to find each path from root to leaf node using Java program. A binary tree is a non-linear data structure type tree with at most two children for each parent. Every node in a binary tree has a left and right reference along with data. The type of data can be as per the application’s requirement. The node at the top of the tree…

Continue

How to find Common, Uncommon, Unique Elements in Two ArrayLists using Java

Here I am going to show you how to find common, uncommon, unique elements in two Lists or ArrayLists using Java program. In this program I will show you how to find common, uncommon, unique string elements as well as object elements in two ArrayLists. For identifying the unique string elements is very straight forward but for objects you need to override the equals() and hashCode() methods in your object class otherwise you won’t be able to find. When you are overriding equals() and hashCode() methods then you should consider…

Continue

How to create Custom Immutable Class in Java

Introduction In this tutorial I am going to show you how to create a custom immutable class in Java programming language. Immutable objects are those objects whose states cannot be changed. For example, you can change the state of an object by changing its attribute’s or field’s value. String is an example of the immutable class in Java. There are other examples of immutable class in Java, such as, all primitive wrapper classes are immutable classes – Integer, Byte, Long, Float, Double, Character, Boolean, Short. In Java 8, the Date…

Continue

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

How to mask a Field or an Attribute in Java

Here I am going to show you how to mask a field or an attribute in Java programming language. This kind of situation occurs when you are dealing with mainly debit or credit cards. So you have a POJO or model class for your card (debit or credit) but for security reason you do not want to display the card number, then you need to mask the card number. Masking something means you want to replace the actual value with some other value, for example, your card number is 3456…

Continue

Junit 5 Mockito Verify Example

In this example I am going to show you how to verify a method has been executed at least. Why do you need to verify method execution? Consider the void method, in your Java class, which you want to perform Junit test case on it and you don’t have any way to tell whether your method successfully tested or not. So here you are left with only one option, i.e., to check whether your method was executed or not. If your method was executed then you know that your code…

Continue

Junit 5 Expected Exception using Assertions.assertThrows()

This example will show you how to work with expected exception using Junit 5 Assertions.assetThrows(). Junit 4 (at least 4.7 version) provides expected attribute with @Test annotation, where you can mention what exception you are expecting from your test method but Junit 5 does not provide such thing. Junit 5 provides assertThrows() that can be used to check the expected exception. A Java unit test should verify correct exception thrown in exceptional case and no exception should be thrown in normal case. assertThrows() asserts that execution of the supplied executable…

Continue

Mock an Autowired Value Field in Spring with Junit 5

In this example I am going to show you how to mock an autowired field in Spring boot framework with Junit 5. When you use @Value annotation in Spring beans to read the value from properties file and later when you want to perform Junit test then you also need to pass value for this autowired field value otherwise you won’t be able to cover your testing. Read if you need Junit 4 version of testing for Spring’s autowired field value. Now it is really cumbersome to place a properties…

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