This is an example of what I want to do:
Class Example {
private Set<A> setA = new HashSet<A>();
private Set<B> setB = new HashSet<B>();
//setters/getters
. . .
// this method is where I don't know what to put
public boolean addToList(Set<?> set, <?> generic){
set.add(generic)
}
public static void main(String[] args){
Example ex = new Example();
ex.addToList(ex.getSetA(), new A());
ex.addToList(ex.getSetB() new B());
}
}
I want to write a method to execute over sets of different datatypes. I find that this will be a little more practical than writing add/get/remove methods for each of a dozen sets.
Is there a way to do this in Java?
I think you need parameterized method
public <T> boolean addToList(Set<T> set, T generic){
set.add(generic)
}