Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

267
Views
Cannot overload method?

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.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report

0

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

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!