Success is the sum of small efforts, repeated.

Tips of Java

One-line code to build and add init value to a list

List<String> list = Collections.singletonList(YOUR_VALUE)

How to convert list of object to list of String

You can call Object::toString to do the conversion


Annotation retention policy

What is Retention policy in java annotations?

A retention policy determines at what point annotation should be discarded. Java defined 3 types of retention policies through java.lang.annotation.RetentionPolicy enumeration. It has SOURCE, CLASS and RUNTIME. Annotation with retention policy SOURCE will be retained only with source code, and discarded during compile time. Annotation with retention policy CLASS will be retained till compiling the code, and discarded during runtime. Annotation with retention policy RUNTIME will be available to the JVM through runtime. The retention policy will be specified by using java built-in annotation @Retention, and we have to pass the retention policy type. The default retention policy type is CLASS.

overload and override

“selection among overloaded methods is static, while selection among overridden methods is dynamic. ”

“a method is overridden when a subclass contains a method declaration with the same signature as a method declaration in an ancestor.”

// Broken! - What does this program print?
public class CollectionClassifier {
    public static String classify(Set<?> s) {
    return "Set";

public static String classify(List<?> lst) {
    return "List"; 

public static String classify(Collection<?> c) {
    return "Unknown Collection";

public static void main(String[] args) {
    Collection<?>[] collections = {
    new HashSet<String>(),
    new ArrayList<BigInteger>(),
    new HashMap<String, String>().values()

for (Collection<?> c : collections)

You might expect this program to print Set, followed by List and Unknown Collection, but it doesn’t. It prints Unknown Collection three times. Why does this happen? Because the classify method is overloaded, and the choice of which overloading to invoke is made at compile time.”

“the best way to fix the program is to replace all three overloadings of classify with a single method that does an explicit instanceof test:

public static String classify(Collection<?> c) {
return c instanceof Set? "Set" :
 c instanceof List ? "List" : "Unknown Collection";

Because overriding is the norm and overloading is the exception, overriding sets people’s expectations for the behavior of method invocation. As demonstrated by the CollectionClassifier example, overloading can easily confound these expectations. It is bad practice to write code whose behavior is likely to confuse programmers. This is especially true for APIs.”

“Exactly what constitutes a confusing use of overloading is open to some debate. A safe, conservative policy is never to export two overloadings with the same number of parameters. If a method uses varargs, a conservative policy is not to overload it at all,”

“Functional programming is a term that means different things to different people.” “At the heart of functional programming is thinking about your problem domain in terms of immutable values and functions that translate between them.”

Target typing

“What is implicit in all these examples is that a lambda expression’s type is context dependent. It gets inferred by the compiler. This target typing isn’t entirely new, either.”

“This restriction is relaxed a bit in Java 8. It’s possible to refer to variables that aren’t final; however, they still have to be effectively final. Although you haven’t declared the variable(s) as final, you still cannot use them as nonfinal variable(s) if they are to be used in lambda expressions. If you do use them as nonfinal variables, then the compiler will show an error.”

Functional interface

“A functional interface is an interface with a single abstract method that is used as the type of a lambda expression.”