Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Container of Generic Types in java

Tags:

java

I have a generic class Foo<T> and parameterized types Foo<String> and Foo<Integer>. Now I want to put different parameterized types into a single ArrayList. What is the correct way of doing this?

Candidate 1:

public class MMM {
    public static void main(String[] args) {
        Foo<String> fooString = new Foo<String>();
        Foo<Integer> fooInteger = new Foo<Integer>();

        ArrayList<Foo<?> > list = new ArrayList<Foo<?> >();
        list.add(fooString);
        list.add(fooInteger);

        for (Foo<?> foo : list) {
            //  Do something on foo.
        }
    }
}
class Foo<T> {}

Candidate 2:

public class MMM {
    public static void main(String[] args) {
        Foo<String> fooString = new Foo<String>();
        Foo<Integer> fooInteger = new Foo<Integer>();

        ArrayList<Foo> list = new ArrayList<Foo>();
        list.add(fooString);
        list.add(fooInteger);

        for (Foo foo : list) {
            //  Do something on foo.
        }
    }
}
class Foo<T> {}

In a word, it is related to the difference between Foo<?> and the raw type Foo.

Update:

Grep What is the difference between the unbounded wildcard parameterized type and the raw type? on this link may be helpful.

like image 873
Cyker Avatar asked Jun 30 '13 16:06

Cyker


People also ask

What are the different types of generic boxes in Java?

A Generic Version of the Box Class This introduces the type variable, T, that can be used anywhere inside the class. As you can see, all occurrences of Object are replaced by T. A type variable can be any non-primitive type you specify: any class type, any interface type, any array type, or even another type variable.

What is generic container tag?

Generic containers are just containers that serve no special purpose. Mostly a style attribute is bounded to a generic container for specifying the rendering or using a CSS rule. The only generic containers are <div> and <span> .

Is ArrayList generic type?

Both ArrayList and vector are generic types.

How can you bound generic types?

Whenever you want to restrict the type parameter to subtypes of a particular class you can use the bounded type parameter. If you just specify a type (class) as bounded parameter, only sub types of that particular class are accepted by the current generic class. These are known as bounded-types in generics in Java.


2 Answers

List represents a list with no type parameter. It is a list whose elements are of any type, e.g. the elements may be of different types. It's basically the same as List<Object>.

Meanwhile, List<?> represents a list with an unbounded type parameter. Its elements are of a specific, but unknown (before runtime), type. These elements all have to be from the same type at Runtime.

I recommend avoiding using raw types. The proper solution for your example is List<Foo<?>>.

like image 123
Konstantin Yovkov Avatar answered Nov 15 '22 18:11

Konstantin Yovkov


You want to avoid raw types whenever possible. The correct way to do it would be with
List<Foo<?>>.

like image 34
Jeffrey Avatar answered Nov 15 '22 18:11

Jeffrey