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 ;
}
Set<T>, possibly with constraints based on what you do with the contents.