Swing vs. SWT – Side-by-side comparison

In many articles, developers have compared Swing with SWT with a lot of details. However, there is not a side-by-side comparison by using a diagram which may give a direct impression of how they are different. In this post, I will compare Swing and SWT side by side and get a taste of the difference … Read more

LeetCode – Next Closest Time (Java)

Given a time represented in the format “HH:MM”, form the next closest time by reusing the current digits. There is no limit on how many times a digit can be reused. You may assume the given input string is always valid. For example, “01:34”, “12:09” are all valid. “1:34”, “12:9” are all invalid. Example 1: … Read more

Why do we need Generic Types in Java?

Generic types are extensively used in Java collections. Why do we need Generic types in Java? Understanding this question can help us better understand a lot of related concepts. In this article, I will use a very short example to illustrate why Generic is useful. 1. Overview of Generics The goal of implementing Generics is … Read more

LeetCode – Repeated String Match (Java)

Given two strings A and B, find the minimum number of times A has to be repeated such that B is a substring of it. If no such solution, return -1. For example, with A = “abcd” and B = “cdabcdab”. Return 3, because by repeating A three times (“abcdabcdabcd”), B is a substring of … Read more

LeetCode – Permutation in String (Java)

Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. In other words, one of the first string’s permutations is the substring of the second string. For example: Input: s1 = “ab” s2 = “eidbaooo” Output: True Explanation: s2 contains one permutation of s1 (“ba”). Java … Read more

Evaluate math expression with plus, minus and parentheses (java)

Given a string of math expression, such as 1-(2+3), evaluate the value. The expression contains only digits, +, – and parentheses. Java Solution import java.util.ArrayList; import java.util.Stack;   public class ExpressionEvaluator { static class Node { Boolean isPositive; Integer value; ArrayList<Node> list;   public Node(boolean isPositive, Integer value) { this.isPositive = isPositive; this.value = value; … Read more

LeetCode – Backspace String Compare (Java)

Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character. Example 1: Input: S = “ab#c”, T = “ad#c” Output: true Explanation: Both S and T become “ac”. Example 2: Input: S = “a##c”, T = “#a#c” Output: true Explanation: Both … Read more

Java Method to Shuffle an Array

How to shuffle an array in Java? There are two approaches to shuffle an int array(randomizes the order of the elements in an array), one is to use the Collections.shuffle() method, the other is to manipulate array elements. Approach 1: Shuffle elements in an array This is flexible, and easy to be changed to fit … Read more

How Static Type Checking Works in Java?

From Wiki: Static type-checking is the process of verifying the type safety of a program based on analysis of a program’s source code. Dynamic type-checking is the process of verifying the type safety of a program at runtime Java uses static type checking to analyze the program during compile-time to prove the absence of type … Read more

Comparable vs. Comparator in Java

Comparable and Comparator are two interfaces provided by Java Core API. From their names, we can tell they may be used for comparing stuff in some way. But what exactly are they and what is the difference between them? The following are two examples for answering this question. The simple examples compare two HDTV’s size. … Read more

Monitors – The Basic Idea of Java Synchronization

If you took operating system course in college, you might remember that monitor is an important concept of synchronization in operating systems. It is also used in Java synchronization. This post uses an analogy to explain the basic idea of “monitor”. 1. What is a Monitor? A monitor can be considered as a building which … Read more

Java Code – Convert a file to a String

How to read file content into a string? The following is the Java code to do that. To make it work, the filePath need to be changed. public static String readFileToString() throws IOException { File dirs = new File("."); String filePath = dirs.getCanonicalPath() + File.separator+"src"+File.separator+"TestRead.java";   StringBuilder fileData = new StringBuilder(1000);//Constructs a string buffer with … Read more