Java – erasure of method is the same as another method in type

Advertisements

How to get rid of the error
"erasure of method is the same as another method in type"

without changing methods name

public static List<String> convert(List<String> list, Function<String, String> convertFunction) {
            
            return list;
//proper code
            
        }
        
        public static List<Map<String, String>> convert(List<Map<String, String>> list, Function<String, String> convertFunction) {
            return list;
//proper code
        }

>Solution :

You can’t have two methods with parameters which differ from one another only with their generic type because List<String> and List<Map<String, String>> exist only at compile time.

At runtime, there would be List<Object> and Function<Object, Object>.

That’s how generics were implemented in Java, and you can’t do anything with it.

But you can combine these two methods in one:

public static <T, R> List<R> convert(List<T> list,
                                     Function<T, R> convertFunction) {
    
    return foo;
}

Leave a ReplyCancel reply