In Java is there anyway to have one constructor that will accept an array or a collection? I have been fiddling with this for a while but I don't think it is possible.
I would like to be able to initialize MyClass
, like this:
MyClass c = new MyClass({"rat", "Dog", "Cat"});
And like this:
LinkedList <String> l = new <String> LinkedList();
l.add("dog");
l.add("cat");
l.add("rat");
MyClass c = new MyClass(l);
This is what MyClass looks like. What can I make XXX be so that this will work? I know that I could overload the constructor, but if I can minimize code that would be awesome right?
public class MyClass{
private LinkedHashSet <String> myList;
public MyClass(XXX <String> input){
myList = new LinkedHashSet <String> ();
for(String s : input){
myList.put(s);
}
}
}