-1

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?

user3466773
  • 176
  • 1
  • 2
  • 14
  • `public boolean addToList(Set set, T generic){` should work, but maybe you could use a map (class as key and your Set as value, ie. A.class => setA, B.class => setB) and addToList becomes `map.get(generic.class).add(generic)` –  Nov 08 '17 at 14:46
  • 1
    Why do you need anything more than `ex.getSetA().add(new A())`? – Andy Turner Nov 08 '17 at 14:48
  • @Andy Turner I could also be adding new B depending on the user's query. This is only a simplified example. I'm dealing with many sets and just wanted something cleaner than writing the same methods over again to handle each datatype – user3466773 Nov 08 '17 at 14:52
  • 1
    You need to show a more illustrative example, because for what you've got here, you don't need to write the same methods over again. – Andy Turner Nov 08 '17 at 14:53
  • I disagree. Examples are supposed to be trivial but inductive. Anything I can do with 2 sample sets I can do with 100, but it's much simpler to look at the smaller sample – user3466773 Nov 08 '17 at 15:06

2 Answers2

3

I think you need parameterized method

public <T> boolean addToList(Set<T> set, T generic){
      set.add(generic)
}
  • Could I ask for more clarification on this method declaration? Particularly the part in "public boolean" – user3466773 Nov 08 '17 at 14:54
  • @user3466773 it's a type variable. It's just saying that generic has to be the same as (or a subtype of) the type of the elements in the set. – Andy Turner Nov 08 '17 at 14:54
  • 1
    As Andy Turner says. The compiler will just make sure that when you call this method if you pass a Set you will pass an instance of T as well, T being any type possible – ThePoltergeist Nov 08 '17 at 14:57
1

(Unless you want to make something if the add method succeeds / fails, you should use void instead of your generic type.)

Here is an example that should work:

public void addToAList(Set<A> set, A generic){
      set.add(generic);
}

However, if you want to make a generic addToList function, you need to push it way further, as you'll need to compare classes between the Set and the Object you are inputting in your method. Considering that you cannot call getClass() or .class on an Object, I think your easiest (but not the best at all) way to do here is to cut it down into two methods.

Yassine Badache
  • 1,810
  • 1
  • 19
  • 38
  • Yeah, I figured I'd have to do something more for checking the values themselves, but I really just needed to know how to construct the method I wanted to handle sets of varying datatypes – user3466773 Nov 08 '17 at 15:11