user3281637
user3281637

Reputation: 13

HashSet of integers

public class MyClass<Integer> extends AnotherOne<Integer> {
    public MyClass(HashSet<Integer> ar) {    
        super(ar);
    } 
}

AnotherOne is an abstract(generic) class which has a constructor which gets HashSet<T> as a parameter. Then it has another method which uses the T type for its parameters.

Everything is ok but I have to use Integer as a parameter to override that method and using Integer instead of int seems weird. Is there a way to use the primitive int?

Upvotes: 1

Views: 1123

Answers (2)

rgettman
rgettman

Reputation: 178293

No, generics cannot have a primitive type as the generic type parameter; it must be a reference type, such as Integer.

Additionally, you are declaring a generic type parameter Integer that is now hiding the actual java.lang.Integer class. If MyClass wasn't itself meant to be generic, then remove it from the class:

public class MyClass extends AnotherOne<Integer>

If it was, use a single capital letter for the generic type parameter:

public class MyClass<T> extends AnotherOne<T>

Upvotes: 4

peter.petrov
peter.petrov

Reputation: 39477

No, you cannot use primitive int for a HashSet.

This is valid for all other generic collection classes too.

Upvotes: 5

Related Questions