Marvin :
Why isn't overloading with a different generic type as a parameter allowed in Java?
For example,
private static String convertListToString(List<Character> list)
and
private static String convertListToString(List<String> list) {
causes an error. Why does Java does this and are there any safe and popular workarounds?
Related
Does exactly what I need, but in a different language - Overloading a method that takes a generic list with different types as a parameter
Zack R :
As @Themelis stated, you cannot do that because they are fundamentally the same parameter. However, you can get around this by utilizing the generics like so:
private static <T> String convertListToString(List<T> list) {
// Your code here.
}
Inside of the method body, you could then loop through and call the toString method on the generics.
String s = "";
for (int i = 0; i < list.size(); i++) {
s += list.get(i).toString();
}
return s;
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=327386&siteId=1