Davis8988
Davis8988

Reputation: 696

How to create a generic <T> class so that T extends Collections<String>?

I want to create a generic ThreadWorker class that it's generic type <T> accepts classes that extends Collection class of type <String>.

I tried:

public class Worker <T extends Collections<String>> extends SwingWorker<VectorDetails , String>    

But I get an error:

The type Collections is not generic; it cannot be parameterized with arguments <String>

Also when I write:

public class Worker <T extends Collections> extends SwingWorker<VectorDetails , String>  

It compiles fine, but thats not what I need since instances of this Worker class can be created with types of other classes which are not String. (Like < Integer> , <Character> , etc..).

How can I force the user to create instances of my class with types that extends/implements Collection class that holds Strings?

Upvotes: 0

Views: 464

Answers (2)

Amudhan
Amudhan

Reputation: 725

The error says what is wrong in the code. Collections is not a generic type. It is a utility class with a lot of static methods to manipulate Collection(s). You should use Collection<String> or any type. Collection is a generic type (which lists and sets extend).

Upvotes: 1

Beastmaster
Beastmaster

Reputation: 386

Is that what you are looking for?

class StringCollection<T extends Collection<H>, H extends String> {}

Upvotes: 1

Related Questions