0

I'm writing a generic interface representing a processor for several different types of custom objects, each of which is identified by either a string ID or an integer ID. I have written it in a way where T represents my custom objects (which all inherit from the same abstract class), but I need to take the IDs as parameters generically as well. Is there a way to have two generic types in a single class in Java?

I tried to write the simplest possible example of what I am trying to do below. "String/Integer" represents the two possible types of ID, which depends on what T is.

public interface Processor<T> {

    T update( String/Integer id );

    T find( Integer/Integer id );

    List<T> findAll( List<String/Integer> ids );

}

Thanks in advance for any advice!

noob
  • 5
  • 4
  • It seems like the questions are different, but the accepted answers definitely overlap. Thanks Gaurav – noob Sep 11 '19 at 05:10

1 Answers1

2

Yes you can have multiple generic types.

public interface Processor<T, S> {
    T update( S id );
    T find( S id );
    List<T> findAll( List<S> ids );
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249