简体   繁体   中英

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?

No, you cannot use primitive int for a HashSet.

This is valid for all other generic collection classes too.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM