I’m looking for a way so that I can use the same portion of code to deal with parameter passing through Set or variadic parameters. e.g.
public void func(String...strs){
for (String str : strs){
//Deal with str
}
}
According to the spec, the func will also supports:
public void func(Set<String> strs){
for (String str : strs)
//Deal with str
}
}
}
Both the processing code are identical, how can I merge to a single implementation? Please kindly advise.
Thank you.
Regards,
William
There is no efficient solution to the problem you pose. Varadic functions receive an array. No magic wand will wave itself and convert from
java.util.Setto array.Further, the semantics don’t match, since Set is unordered and forbids duplicates, and array is ordered and permits dups.
you can define the second function as
and pay for the allocation.