'Convert a method to a generic one to avoid duplicate code

I need some help with the following: I have multiple calls to a method that look like this:

private void saveA(myObjA myObj, List<A> myList) {
    if (myList != null && !myList.isEmpty()) {
        myObj.saveAll(myList);
    }
}

private void saveB(myObjB myObj, List<B> myList) {
    if (myList != null && !myList.isEmpty()) {
        myObj.saveAll(myList);
    }
}

...

Example of interface:

public interface myObjA
    extends JpaRepository<A, Long> {

}

public interface myObjB
    extends JpaRepository<B, Long> {

}

...

The thing is I'm creating a new one for all the other calls (myObjB, myListB, myObjC, myListC). myObj is actually an interface and the second parameter is always a list of some object. Is there any way to convert this method to a single one and specify the object type in the call?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source