How to run JUnit test with Maven

java-featured-image

This tutorial will cover how to run Unit tests using the Maven’s Surefire plugin. If you are not familiar with Unit Testing, you can follow this tutorial for a quick catch-up. In our Maven Project, we need the following mandatory dependencies: <dependencies>     <dependency>         <groupId>org.junit.jupiter</groupId>         <artifactId>junit-jupiter-api</artifactId>         <version>5.4.2</version>         <scope>test</scope>     </dependency>     <dependency>        <groupId>org.junit.jupiter</groupId>         <artifactId>junit-jupiter-engine</artifactId>         <version>5.4.2</version>         <scope>test</scope>     </dependency> </dependencies>…

Continue reading

Java Parallel Streams Example

java-featured-image

Stream API allows developers to  take advatange of multi core architectures and improve the performance of Java program by  creating parallel streams and making your program perform operations faster. There are two ways of creating a parallel stream: by using the parallelStream() method by using the parallel() method When you are using parallel streams, effectively…

Continue reading

ThreadLocal Java Example

java-featured-image

ThreadLocal is a class that provides thread local variable and is used to achieve thread safety. The data stored will be accessible only by a specific thread. ThreadLocal extends Object class and provides thread restriction which is a “part” from local variable. Creating a ThreadLocal variable ThreadLocal threadLocalExample = new ThreadLocal(); The instantiation of the…

Continue reading

Java hashCode() Method Example

java-featured-image

The super class in Java java.lang.Object provides two important methods for comparing objects: equals() and hashcode(). These methods are widely used when faced against implementing an interaction between classes. In this tutorial, we are only going to look at hashCode(). Method Definition and Implementation hashCode(): By default, this method returns a random integer that is unique every time. If you execute…

Continue reading

Choose the Right Java Collection

Java offers you a variety of collection implementations to choose from. In general you will always look for the collection with the best performance for your programming task, which in most cases is ArrayList, HashSet or  HashMap. But be aware, if you need some special features like sorting or ordering you may need to go for a special…

Continue reading

Java ArrayList Example

java-featured-image

Java ArrayList class is a resizable array that implements the List interface. It permits all elements, including null and also implements all optional list operations. Most operations that can be run on ArrayList such as size, isEmpty, get, set, iterator and listIterator are all constant time. However, the add operation’s complexity is O(n) time. Compared to LinkedList, the…

Continue reading

Java LinkedHashSet Example

java-featured-image

LinkedHashSet class in Java differs from HashSet as its implementation maintains a doubly-linked list across all elements. This linked list defines the iteration ordering which is the order in which the elements were inserted into the set. It is called an insertion-order. If an element is re-inserted into the set, the insertion order is not affected by…

Continue reading

Java ConcurrentHashSet Example

java-featured-image

Java 8 finally allows us, the programmers, to create thread-safe ConcurrentHashSet in Java. Before then, it was simply not possible. There were variations that tried to improvise the implementation of the above-mentioned class, one of which would be, using ConcurrentHashMap with a dummy value. However, as you might have guessed, all of the improvisations of…

Continue reading

Java LinkedHashMap Example

java-featured-image

LinkedHashMap is a combination of hash table and linked list that implement the Map interface with predictable iteration order. The difference between HashMap and LinkedHashMap is that LinkedHashMap maintains a doubly-linked list which allows scanning through all of its entries back and forth. The order is maintained, meaning the order in which keys were inserted…

Continue reading

Java EnumMap Example

java-featured-image

EnumMap class implements the Map class and enables the use of enum type keys. Enum maps are maintained in the natural order of their keys. It is important to note that null keys are not permitted. If attempt has been made for adding a null key, NullPointerException will be thrown. However, even though null keys are not allowed, null values are. Since all…

Continue reading

Java IdentityHashMap Example

java-featured-image

IdentityHashMap implements the Map interface and two keys are considered equal when checked as k1==k2 (not by using equals method). This itself violates the Map‘s general contract, which means that IdentityHashMap is clearly not a general-purpose Map implementation. There are only so many cases where this class would be useful. IdentityHashMap permits both null values and the null key, alongside all optional map…

Continue reading

Java ConcurrentMap Example

java-featured-image

Even though Map is implemented by many classes, many of them are not thread-safe or some of them are but not efficient. This is why ConcurrentMap was introduced in Java 1.5. It is thread-safe and efficient. Overridden default implementations: compute replaceAll forEach getOrDefault computerIfAbsent computerIfPresent ConcurrentMap consists of an array of nodes that are represented as table…

Continue reading

Depth-First-Search Example Java

java-featured-image

Searching and/or traversing are equally important when it comes to accessing data from a given data structure in Java. Graphs and Trees are an example of data structures which can be searched and/or traversed using different methods. Depth-first-search, DFS in short, starts with an unvisited node and starts selecting an adjacent node until there is…

Continue reading

Breadth-First-Search Example Java

java-featured-image

Searching or traversing is really important when it comes to accessing data from a given data structure. There are different methods of traversing/searching elements within these data structures such as Graphs and Trees. Breadth-first search is one example of these methods. BFS is an algorithm that traverses tree or graph and it starts from the…

Continue reading

Different Algorithm Time Complexities

java-featured-image

Before starting to explain the different time complexities, let’s first look at what an actual algorithm is. The formal definition of an algorithm is “a process or set of rules to be followed in calculations or other problem-solving operations, especially by a computer.”. So in other words, an algorithm is a defined path which is…

Continue reading

Java Comparator Example

java-featured-image

in this tutorial, we will discuss java comparator and its examples. What is Java Comparator? Java Comparator is an interface for arranging Java objects. Summoned by “java.util.comparator,” Java Comparator analyzes two Java protests in a “compare(Object 01, Object 02)” group. Utilizing configurable strategies, Java Comparator can contrast objects with profit a number based for a…

Continue reading

Introduction to Docker and Docker containers in Java

java-featured-image

In short, Docker is a tool that allows you to build, deploy and run applications easily by the usage of so-called containers. These containers let us package all the essentials such as libraries and dependencies. In addition, the containers run on the host operation system. There are many benefits that come when we use Docker. It…

Continue reading

Installing and configuring MySQL database and server for Spring usage

spring-featured-image

In the end of this tutorial, you will have installed the right MySQL products needed to develop applications in Spring. You will need two essential things: MySQL Database and MySQL Server. Follow the steps below. Configuring the MySQL Database Visit https://www.mysql.com/downloads/ and then select Community downloads, like so: On the next page, you will see…

Continue reading

How to use the MySQL connector in Java

java-featured-image

Before testing the MySQL connection from a Java program, we’ll need to add the MySQL JDBC library to the classpath.  We will need to download the mysql-connector-java-*.jar file from the downloads page: Now, depending upon your work environment (e.g. Eclipse or Command line) you will have to do either one: If working with Eclipse IDE,…

Continue reading