0

What is the best way in java to deal with three methods in same class with same implantation but different object type as a parameter
as I don't like to duplicate the code
is it good or possible to have one method with parameter as Set < Object >

EX :

public static String M1(Set<Book> set) {
    Iterator<Book> first = set.iterator();
    String id1 = "";
    while (first.hasNext()) {
         id1 = first.next().getName();
        if (id1.equalsIgnoreCase("ABC")) {
    return id1 ;
        }
    }
    return id1 ;

}

public static String M2(Set<Student> set) {

Iterator<Student> first = set.iterator();
    String id1 = "";
    while (first.hasNext()) {
         id1 = first.next().getName();
        if (id1.equalsIgnoreCase("ABC")) {
    return id1 ;
        }
    }
    return id1 ;

}

public static String M3(Set<Course> set) {

    Iterator<Course> first = set.iterator();
    String id1 = "";
    while (first.hasNext()) {
         id1 = first.next().getName();
        if (id1.equalsIgnoreCase("ABC")) {
    return id1 ;
        }
    }
    return id1 ;

}
9
  • 1
    All of those methods have the same erasure, so you can't do it via overloading without specifying additional arguments to differentiate between the various methods. But if the body is identical, why do you need overloads in the first place? Commented Jan 13, 2020 at 11:43
  • 1
    It's hard to answer that without knowing what the implementation is. I suspect you actually want it to be generic and accept a Set<T>, possibly with constraints based on what you do with the contents. Commented Jan 13, 2020 at 11:44
  • 1
    implantation? ;-) Commented Jan 13, 2020 at 11:45
  • Could you paste your code for M1() method, what features will be same for those three classes, what is your usecase Commented Jan 13, 2020 at 11:50
  • Note that these methods return the last name in the set, if nothing is found. Is that really intended? Commented Jan 13, 2020 at 12:00

2 Answers 2

3

Ideally, your Book, Student and Course classes would implement a common interface which provides access to the getName() method. Then you can do this:

public static String M(Set<? extends HasName> set) {

    Iterator<? extends HasName> first = set.iterator();
    String id1 = "";
    while (first.hasNext()) {
         id1 = first.next().getName();
        if (id1.equalsIgnoreCase("ABC")) {
    return id1 ;
        }
    }
    return id1 ;

}

Note that an enhanced for loop would be neater here:

String id1 = "";
for (HasName n : set) {
  id1 = n.getName();
  if (id1.equalsIgnoreCase("ABC")) { return id1; }
}
return id1;

If they don't, you can still define a single method which holds the logic:

public static <T> String common(Set<T> set, Function<? super T, String> extractor) {

    Iterator<T> first = set.iterator();
    String id1 = "";
    while (first.hasNext()) {
         id1 = extractor.apply(first.next());
        if (id1.equalsIgnoreCase("ABC")) {
    return id1 ;
        }
    }
    return id1 ;

}

You then need either to call this directly:

String fromBook = common(setOfBooks, Book::getName);

or provide methods to invoke the common method:

String M1(Set<Book> set) {
  return common(set, Book::getName);
}

String M2(Set<Student> set) {
  return common(set, Student::getName);
}

String M3(Set<Course> set) {
  return common(set, Course::getName);
}

Note that these have to have different names, because of type erasure.

Sign up to request clarification or add additional context in comments.

Comments

1

Well, from some point of view yes, you can.Although additional info is required, generics is a way to go. You can have one method with signature like:

public <T> String M1(Set<T> t){
    return t.toString();
}

Although please note, that you are either limited to Object's method, or will have to declare an interface with common functionality and instead of <T> write

<T extends InterfaceName>

2 Comments

You don't need a type variable if it's only involved in the parameters: Set<?> t or Set<? extends InterfaceName> is (usually) sufficient.
Yep, my bad, originally read question without "Set", but rather as Student, Book and etc. in methods, so hastily rewrote solution:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.