Question

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?

Was it helpful?

Solution 2

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>

OTHER TIPS

No, you cannot use primitive int for a HashSet.

This is valid for all other generic collection classes too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top