In this blog, I am going to write a code which will print all the sub-arrays of an array in linear time, i.e O(n). First, we should to try to understand the problem we are trying to solve here. We have an array of integers. {1, 2, 3, 4, 5} and we need to print […]
Watchout for NullPointerException while using method reference in Java, and Learn how you can fix it.
As a Java developer, I love to use method reference whenever I get an opportunity to do it. But, most of us don’t know that we can get NullPointerException , if we are not careful. Let us see an example. I have a function, which takes a supplier, and it prints the value supplied by the supplier. static […]
Use object oriented and functional programming to write a branch-less code for fizz-buzz test
Fizz-Buzz is a some kind of test used in interviews, which I have heard. But, I was never asked this question. In Fizz-Buzz test, what you have to do is to write a code, to print Fizz, Buzz, FizzBuzz or a number. To go in detail, you have to loop over some number or test […]
Let user handle null case, using functional programming in modern java
When pass an object to a function, then there is a scenario which always arises. That, the passed object could be null. And, we have to write logic to react to that null-condition. And, generally that logic is hard-coded in the function. And, there are times as a user I feel that, instead of throwing an exception […]
How to create basic java plugin using Plugin interface, and run it is using -Xplugin command line argument
We can use Plugin interface to create a plugin that runs during compilation of java files. You can watch me code this plugin on youtube. We use com.sun.source.util.Plugin from the jdk. We will have to provide an implementation for init-function of this interface. And, another thing that is important is to provide implementation for […]
Java Data Structure Binary Tree : Print The Nth Level
Printing Nth Level in a binary tree is the same as doing level order traversal. Logic : We loop through all the levels. And, we maintain the current level count. If the level count is the same as the level requested by the user then print the level information. You can watch me code the […]
FUNCTIONAL PROGRAMMING : Currying In Java, How to curry a hashmap
Currying is just a name for a function, which does not have all the arguments passed to it. So, if have a function which takes 4 arguments, we will provide only 3 and we will provide 1 later on. This stage of function, where it does not have all the arguments is called curried stage, […]
Java Module System – How to create a module, compile it and run the modular java application from a command line.
In this post, I am going to tell you how to create a module. How you can compile that module and use modular architecture to run java application. you can watch me code the module-system mentioned in this post on youtube. I am using java 16 and ubuntu 18.04. You can run commands which are […]
Write java code to find that a given binary tree is a subtree of another binary tree
There are two binary tree and you have to find that , one of the binary tree is the sub-tree in another binary tree. We have three cases here. The root of subtree matches with the root of binary tree. If above condition fails, We look into left-subtree of the main binary tree to find […]
Java code to find diameter of a binary tree
Diameter of a binary tree is the maximum width at any node level. The maximum width can be at last level and that can be the diameter of a binary tree. You can watch me code this solution on youtube. We need three values to find the diameter of a binary tree. diameter of left-subtree […]