In this article, we are going to cover one of the essential topics of java, null handling ,and we are going to do this with Optionals that was introduced in Java 8 and we will be doing that in full detail with examples.

We are going to create Optional returning methods and also see the Spring way of using Optionals for database operations. Firstly, let’s remember what NullPointerException is and then look at what Optionals are.

In Java, if we want to create a variable, we first declare it and then initialize it. When we use a reference that points…


In this post, I am going to talk about loading initial data to our database for later use in our Spring Boot application.

Firstly, let’s look at why we may need initial data.

  • First use case is that after application started, we may be in need of data already created in our database for us. Imagine a situation where you scrape a website and collect a huge amount of data and you are planning to use it in the regular flow of the application. …


In this article, we are going to see what other way we can have to achieve singleton implementation and you are propably very familiar with the concept I will talk about- in a different way.

I think you get the clue off the image above. Yes, we are going to use enums to implement singleton structure.

Firstly, let’s take a look at an ordinary singleton implementation and then see why we better off using enums for singleton implementations. The singleton you can find below simple has a private constructor and a static instance of itself. …


In this article, I am going to talk about package structures for Spring Boot applications and give the one I create whenever I started a new project.

Proper packaging in a project is important because when the project grows or someone else is assigned to your project, it must be easy to get around different kind of files. For example, when you need a file of type model, you just directly go into the model package and easily find what you are looking for. If you do no have such a structure, it becomes a time-consuming problem in large projects.


Factory pattern is a creational pattern where all the object creation logic resides in a factory class. In this article, I am going to talk about how to use it in a Spring Boot application. So, let’s start

Imagine we have Viewer class which does as its name suggests viewing operation of different types of media such as document, video, image etc. We have the following model classes which are empty as its logic is not a interest for us.

public class Document {}public class Image {}public class Video {}

So, in our application, we…


In this article, I am going to talk about how we can order different threads so they run in the order we wish them. Of course, there are traditional ways to ensure order but I want to show you an elegant way called CompletableFuture which was introduced in Java 8.

1- What is CompletableFuture?

CompletableFuture is a new way of executing threads that is very simple to use as you will see now. Let’s look at how we can execute a thread with this. Firstly, we create a Runnable class.

public class TextDownloader implements Runnable {

@Override
public…


In this article, I am going to talk about maven’s core parts, Dependencies and DependencyManagement (especially sub moduling wise).

Let’s start with what dependencies section is in a pom file. Dependencies part is where we define our dependencies that we are going to use in our project. For instance, if we want to use Guava utilities in our project we add it to our pom as follows

<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1.1-jre</version>
</dependency>
</dependencies>

There is an import point we are going to use when comparing the two. Dependencies we add under dependencies tag will be directly conveyed to sub…


In this article, I am going to talk about two multi-threading concepts, runnable and callable.

1- What is Runnable?

Runnable is an interface that classes implementing it are going to be executed in threads. Here, you can see the Runnable interface. All your logic that needs to be executed in a thread will be in the overridden run method. You will notice that it is a void method.

@FunctionalInterface
public interface Runnable {
public abstract void run();
}

2- What is Callable?

Everything I wrote about Runnable is valid for the Callable interface except one thing, return type.


Delay queues are thread-safe data structures that you can pull elements off after given delay.

1- How to use delay queue?

Unlike any other queues, delay queue accepts objects of classes that implements Delayed interface which also extends Comparable interface which you can see below.

public interface Delayed extends Comparable<Delayed> {
long getDelay(TimeUnit unit);
}
public interface Comparable<T> {
public int compareTo(T o);
}

So, when you create your own object that will put into delayed queue, you will implement these methods. Let’s create our own Delayed class.

public class DelayedItem implements Delayed {
private String name;
private long startingTime…


Shutdown hooks in Java are simply threads that will execute before normal execution of a program terminates. Let’s look at how and when to use them.

How to create your own shutdown hook?

You can create your custom shutdown hook easily by extending Thread class as your custom hook will also be a thread. Then you need to implement what you need to do inside overriden run method. In the code snippet below, CustomShutdownHook class I created will check if the network connection is still open and will close it if so.

public class CustomShutdownHook extends Thread {
private NetworkService networkService…

Ömer Kurular

I am Ömer and currently working as a full-time software engineer. I will share my knowledge with you I gained through years of professional and self working.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store