Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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
        }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading