Amit Kumar Mondal bio photo

Amit Kumar Mondal

Java Enthusiast

Email Facebook Google+ LinkedIn Github

Overview

Overview

While refactoring old code, you might have already encountered that the classes are inundated with lots of non-generic List. It is important for a codebase to evolve and that’s why I would like to discuss about how to refactor your old non-generic List to generic List.

Example

public final class UnchekedCast {

	public static void main(final String[] args) {
		final List objectss = asList("a", "b", "c");
		final List<Object> objects = asList("a", "b", "c");
		final List<String> strings = ((List<String>) ((List<?>) objects));
		final List<String> stringss = ((List<String>) ((List<?>) objectss));
		System.out.println(strings);
		System.out.println(stringss);
	}

}

Here, you can clearly see to cast a generic

List<Object>

to

List<String>

, we have to take 2 steps. Initially, we have to safely cast it to unbounded wildcard List and then to the actual

List<String>

. The same method applies for non-generic List or raw type List as it is as same as

List<Object>

.

You would also get to see Unchecked Cast warning which informs you about the presence of cast from a generic type to a non-qualified type or the vice versa.