4

I have two methods that do a very similar thing: receives a map and then print it to an output file, except that they receives different data type of the map and Java (IntelliJ, in fact) is not allowing me to overload it and says 'parse(Map<String, Long>)' clashes with 'parse(Map<Set<String>, Integer>)'; both methods have same erasure.

Method A:

private static void parse(Map<String, Long> map) {

        PrintWriter output = null;
        try {
            output = new PrintWriter("output1.csv");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        assert output != null;
        map.entrySet().forEach(output::println);
        output.close();
    }

Method B:

    private static void parse(Map<Set<String>, Integer> map) {

        PrintWriter output = null;
        try {
            output = new PrintWriter("output2.csv");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        assert output != null;
        map.entrySet().forEach(output::println);
        output.close();
    }

I could always make them two different methods with different names but that would make my code looks unnecessary long and wonky, so I am trying to avoid it.

What am I doing wrong here? Please enlighten me.

Mint
  • 428
  • 5
  • 18

6 Answers6

4

that is because of type erasure thingy in java. basically, after compilation, your code will look like this

private static void parse(Map map) {
}

private static void parse(Map map) {
}

looking at the code. why not just make the method signature like this ?

private static void parse(Map<?, ?> map, String fileName) {
}

that way, you would also avoid unnecessary overload.

kucing_terbang
  • 4,991
  • 2
  • 22
  • 28
2

You can Overload a method with DIFFERENT parameters, different number of parameters of different type of parameters. In your example code, Java will view both methods as the same as they have the same input parameter, which is Map, you can add another parameter in one of these methods to make them work.

zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
  • But even though the data type of the maps are different, it still does not count? If that's true I would find it's a really weird thing. It would be great if I can get some further explanation on why is it happening. – Mint Nov 17 '20 at 03:13
  • Overloading happens in the compile time and it's static, compiler decides which method to use by the signature of input parameter(s), for the same parameter signature, which is Map here, it will generate error. – zhangxaochen Nov 17 '20 at 03:20
2

Yes, you can. They are all the same, also, the single Statement where you use the parameter is map.entrySet() which is https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#entrySet--

So you just have to use the Interface Map<V,K>, here is an example of how to implement generis on Map Generic Map Parameter java

sasal
  • 201
  • 1
  • 7
2

It is not possible as both of the methods have same type signature. As mentioned in Wikipedia, In the Java programming language, a method signature is the method name and the number, type and order of its parameters. Return types and thrown exceptions are not considered to be a part of the method signature.

2

Java generics uses type erasure, means generic parameter type not consider by compiler. After type erasure, compiler treats both methods has same method signatures.

private static void parse(Map<String, Long> map)   --> parse(Map map)
private static void parse(Map<Set<String>, Integer> map) -->  parse(Map map)

These two methods can not be overloaded, it's better to change method names or use single method for both files by providing extra argument.

You can refer for more details: Method has the same erasure as another method in type

Satish Varma
  • 256
  • 1
  • 8
2

Actually,your methods has same method signatures. like

// private static void parse(Map<String, Long> map)
private static void parse(Map<K,V> map)

// private static void parse(Map<Set<String>, Integer> map)
private static void parse(Map<K,V> map)
YuHao Zhu
  • 44
  • 3