I did not find a direct answer to this specific question, so... Assume class:
class MyClass {
private final Set<String> tags;
MyClass(Set<String> initialTags) {
this.tags = ???(initialtags);
}
}
I just want a copy without caring about exact implementation of the set, with minimal overhead, so basically a direct copy of whatever internal state initialTags has. I can't use clone
, since Set is interface. Is this possible, or do I have to use something like new HashSet<String>(tags);
, needing to make a decision about the type?
Assumptions: Set item type is resticted to immutable. I can trust the caller to not pass a broken initialTags
Set implementation, and I can trust it to have good enough performance for any internal use in this class.
I'm thinking of trying reflection and clone(), but this seems a bit dirty...