Category Archives: Java Core

Java 8, Java 9

Common parallel stream pitfalls

One of the most powerful features of the Java 8 Streams provides an easier path to parallel programming. However, in some cases, parallel stream may make programs run slower in comparison with the serial stream, including for loop to iterate over a collection. I put some common mistakes when using the Java 8 parallel stream to warn… 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 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: 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 »

Remove duplicated elements from a collection

1. Using LinkedHashSet from a given list Remove duplicated elements by using LinkedHashSet because it contains unique element and in the same order as the List Output in console: 2. Lambda Stream on arbitrary key Output in console:

Exception Handling Tips

Please refer to Basic Exception Handling before reading some exception questions below. Question 1.  Consider the following program: Which one of the following options correctly describes the behavior of this program? a) The program prints the following: InvalidKeyException. b) The program prints the following: RuntimeException. c) The program prints the following: IllegalArgumentException. d) The program prints… Read More »

String v.s StringBuilder/StringBuffer

Strings String objects are immutable, but reference to Strings are not. In addition, because of Strings occupying large amount of a program’s memory, the JMV uses the “String constant pool” – a special area of memory to make more memory. Now we will take into account of a following example below to understand about immutable… Read More »

Basic Exception Handling

This note will cover as following below: Overview. Handle or declare exception. Create your own exception. Checked v.s Unchecked Exception. Throw vs Throws. Common mistakes. Advantage of exception. 1. Overview Figure 1: The Throwable class (source: The Java Tutorials of Oracle). As can be seen from the Figure 1, an exception object is an instance… Read More »

Generics with class and method

Generics is an interface or class that can be used with many different types. Following note will describe generics using in class and method in general. Generic class Defined with format: class Box <T1, T2, …,Tn> { /*…*/} Used:  Box<String, Integer, MyClass, MyInterface> box = new Box<String, Integer, MyClass, MyInterface>(“Hello”,1, car, toy) Noticeable that T type should be… Read More »

Java Collection with Comparators and Comparables

1. Try TreeSet with Comparator and Comparable In Java, TreeSet is a member of the Java Collection Framework. Elements of the TreeSet are ordered by using their natural ordering, or by a Comparator. A Big-O notation of tree but not specific to Java refers to a pretty good website: Java supports two interfaces to… Read More »