Wednesday, December 20, 2017

SendGrid Email API integration with Spring Boot REST Application

SendGrid is a popular cloud-based email delivery service. A few months ago, I had an opportunity to work with this technology along with Spring Boot application to send email to subscribers. Today I am going to develop a very simple Spring Boot REST application to integrate SendGrid email API to send an email to a valid email address.

Sunday, December 17, 2017

Breadth First Search(BFS) In Java

BFS or Breadth First Search is a very well-known graph traversal algorithm. The following java program performs this algorithm using adjacency list representation of graphs. Graphs may contain a cycle. To avoid processing a node twice, we used a Boolean array, name visited.

Friday, December 15, 2017

Generate unique permutation in Java

We already saw how to generate all possible permutation in java. Today we will modify our algorithm to remove duplicate entries to keep only unique permutation.

For an example, if the given string is: 112, our program will generate:

112
121
211

Spring Boot REST API - Convert English Number into Words

Spring is one of the most popular Java EE application framework. Today we will develop a very simple Spring Boot REST API which receives a number as a request and in response, we will get the corresponding English words.

Please note that, this is not a tutorial to learn about Spring Boot REST API. I assumed you already familiar with this technology.

Spring Boot REST API - Spring Bean Scope illustration

We already know that Spring defines 5 types of bean scopes:

a) singleton
b) prototype
c) request
d) session
e) globalSession

Last three scopes are only available on web-aware application. Singleton and prototype are spring’s core scope and singleton is the default scope.


Thursday, November 23, 2017

Sieve of Eratosthenes In Java

The sieve of Eratosthenes is a famous ancient algorithm to find all prime numbers up to a given limit. We are going to implement this algorithm in Java.


Steps we will follow:

a) Lets we have an array of size 25.
b) First start with 2, remove all numbers which are divisible by 2.
c) Find the next number, which is 3. Remove all numbers which are divisible by 3.
d) Next survivor is 5 and we repeat the same procedure.
e) For the given limit 25, we don't need to find anymore survivor after 5, as 5 X 5 = 25. In other words, we only process up to the square root of the limit.
 f) All survivor numbers in the array are Prime. We save those numbers in a separate array.

Generate all possible permutations in Java using recursion.

In the previous example, we drew a pyramid in java without any loop. We used recursion to achieve our goal and hopefully, you got a very clear idea how recursion works. In the following example, I will show you how to print all permutations of a given string. If the given string is "1234", then all possible permutations are:

1234
1243
1324
1342
1324
1342
1423
1432
 .
 .
 .
4321

 A total number of permutations are 24. ( 4! = 24 )

Wednesday, November 22, 2017

Eclipse Paho Java Client (MQTT client) integration with Spring Boot REST API

MQTT is an open, lightweight, simple client-server (publish / subscriber) messaging transport protocol. It is designed with a minimal protocol overhead. On the other hand Eclipse Paho is one of the most popular client library implementations. In the following example, I will show how to integrate Spring Boot with Eclipse Paho.

Tested with the following environment and technologies:

a) JDK 1.8
b) Eclipse
c) Maven
d) Spring Boot
e) Eclipse Paho as MQTT client library.
f) Postman as REST API client (Google Chrome extension)
g) MQTTLens as client tool for MQTT (Google Chrome extension)

Monday, November 20, 2017

Printing Pyramid Without Any Loop In Java

If you are asked to write a java program to print out the following pyramid, I am pretty sure that you will accept the challenge without any doubt.  Wait! Life is not as easy as you think. If  you are given the following restrictions, are you able to solve the problem?:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

Tuesday, November 14, 2017

Pascal Triangle In Java

Pascal Triangle is one of the most interesting number pattern which is a triangular array of the binomial coefficients. The following example shows how to generate this triangle in Java.