# Author Archives: khiem

I am a fan of Java ecosystem. Love reading books, and sharing.

## Creating a URL Shortener with Spring RESTful Web Service, Liquibase, and Postgresql

In this note I will create a URL shortening service using Spring Restful, Spring Boot, Liquibase, and PostgreSQL database.  In a similar way of some shortening services, such as goo.gl, tinyurl.com, and bit.ly, purpose of the shortened URL may be more convenience for website and provide detailed information on clicks a link receives. 1. URL Shortener Service. The service will… Read More »

## Bits Manipulation in Java

1. Detect if two integers have opposite signs. 2. Determining if an integer is a power of 2. 3. Swap the values of a and b without using a temporary variable. 4. Check non-zero. References: Gayle Laakmann Mcdowell, “Bit Manipulation”, in Cracking the code interview, 6th Edition. Bit Twiddling Hacks, Accessed at https://graphics.stanford.edu/~seander/bithacks.html Bit Manipulation… Read More »

## Maps in Java

Concrete maps in Java JDK 1.8 Below is the summary of differences between HashMap, LinkedHashMap, TreeMap, and Hashtable in Java: (Source: javarevisited.blogspot.com) If we need to get the key back in insertion order, then use LinkedHashMap. If we need to get the key back in their true/natural order, then use TreeMap. Otherwise, HashMap is best because it… Read More »

## Find All Elements in an Array which appears more than N/K times, N is Array Size and k is a Number

Similarly, original Boyer-Moore Majority Vote Algorithm defined “Majority element of an array A[1 … n]: An array is said to have a majority element if more than half of its entries are the same.” In other words, the majority element is the element that appears more than ⌊ n/2 ⌋. One difference is finding more… Read More »

## Merge two stored arrays with different sizes.

1. Question. You are given two sorted arrays, A and B, where A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order. For example: int[] a = {2, 3, 4, 8, 10, 100, 0, 0, 0, 0}; int[] b = {1, 4,… Read More »

## Binary search algorithm

1. Getting. In binary search algorithm, we find an element x in a sorted array by using the principle of divide and conquer. First, we compare x to the middle point of the array. If x is less than the middle point, then we search the left of array. Otherwise, we look for the right… Read More »

## Singleton Pattern

1. Definition. Singleton Pattern makes sure that a class only have one instance and provide a global point of access to it. It also belongs to Creation Pattern Group. Singleton Pattern should be used where we only need one of: Thread pools, caches, dialog boxes, object that handle preferences and registry settings, objects used of… Read More »

## Observer Pattern

Observer Pattern defines a one-to-many relationship between a set of objects. When the state of one object changes, all of its dependents are notified/updated automatically. It also belongs to Behavior pattern group. 1. A customized notification system. Several newspaper agencies like CNN, NYTime, BBC are subscribed to a feed of new tweets and may want… Read More »

## Building a Hypermedia-Driven RESTful Web Service with Spring HATEOAS

1. Introduction. This note follows up the rest-hateoas tutorial from Spring which describes the general concept about HATEOAS. According to the Richardson Maturity Model, HATEOAS is considered the final level of REST, which explains that the REST resources contain not only data, but links to related resources. 2. Solution with Spring HATEOAS. From the last… Read More »

## Hibernate cache in theory

The note will follow: 1. Introduction. Hibernate has a two-level cache architecture: first-level cache and second-level cache. Figure 1: Hibernate’s two levels cache (source: Manning) The first-level cache is the persistence context cache (scope of Session). The cache ensures that when the application requests the same persistent object twice in a particular session, it gets back… Read More »