user12019412
user12019412

Reputation:

Why can't you generalize parametric classes?

When you define parametric classes you can only use a fixed number of parameters.

class Container<T> {
    ...
}

However, if you want to create, say, a Map with multiple values. You must use a Map<K, List<V>> instead of Map<K, V1, V2, V3>. Why can't you define something like?

class Map<K, V, ...> {
    ...
}

Upvotes: 2

Views: 48

Answers (1)

Andreas
Andreas

Reputation: 159165

You can, if you implement a Tuple class with 3 elements.

class Tuple3<T1, T2, T3> {
    private final T1 t1;
    private final T2 t2;
    private final T3 t3;

    // constructor, getters, ...
}

Then you can use it:

Map<K, Tuple3<V1, V2, V3>>

It is not the responsibility of Map to support multiple types of values. See separation of concerns (SoC) for more information on that topic.

Upvotes: 3

Related Questions