初始化Map的类型安全的varargs方法

1 java generics variadic-functions

我想写一个可以用来初始化Map的方法.首裁:

Map map(Object ... o) {for (int i = 0; i < o.length; i+=2){result.put(o[i], o[i+1])}}
Run Code Online (Sandbox Code Playgroud)

简单,但不是类型安全的.使用泛型,可能是这样的:

<TKey, TValue> HashMap<TKey, TValue> map(TKey ... keys, TValue ... values) 
Run Code Online (Sandbox Code Playgroud)

但是不支持该语法.所以最终我来到这个:

public static <TKey, TValue, TMap extends Map<? super TKey, ? super TValue>> TMap map(TMap map, Pair<? extends TKey, ? extends TValue> ... pairs) {
    for (Pair<? extends TKey, ? extends TValue> pair: pairs) {
        map.put(pair.getKey(), pair.getValue());
    }
    return map;
}

public static <TKey, TValue> HashMap<? super TKey, ? super TValue> map(Pair<? extends TKey, ? extends TValue> ... pairs) {
    return map(new HashMap<TKey, TValue>(), pairs);
}

public static <TKey, TValue> Pair<TKey, TValue> pair(TKey key, TValue value) {
    return new Pair<TKey, TValue>(key, value);
}

public static final class Pair<TKey, TValue> {
    private final TKey key;
    private final TValue value;
    Pair(TKey key, TValue value) {this.key = key; this.value = value; }
    public TKey getKey() {return key;}
    public TValue getValue() {return value;}
}
Run Code Online (Sandbox Code Playgroud)

但是当我试一试时,我需要施展它:

private static final Map<? extends Class<? extends Serializable>, ? super TypeHandler<? extends Serializable > > validCodeTypes =
    /* (Map<? extends Class<? extends Serializable>, ? super TypeHandler<? extends Serializable >>) */
 map(
    pair(Integer.class,   new IntHandler()),
    pair(Integer.TYPE,    new IntHandler()),
    pair(Character.class, new CharHandler()),
    pair(Character.TYPE,  new CharHandler()),
    pair(String.class,    new StringHandler())
);

private interface TypeHandler<TType extends Serializable> {}

private static class CharHandler implements TypeHandler<Character> {}
private static class IntHandler implements TypeHandler<Integer> {}
private static class StringHandler implements TypeHandler<String> {}
Run Code Online (Sandbox Code Playgroud)

任何人都可以告诉我如何编码我的map()方法,以便它是完全通用的,但不需要被铸造?

Chr*_*ung 6

为了让自己的生活更轻松,请不要使用包含通配符的返回类型.通常,通配符类型仅用于方法参数.

所以,试试这个:

public static <TKey, TValue, TMap extends Map<TKey, TValue>> TMap map(TMap map, Pair<? extends TKey, ? extends TValue>... pairs) {
    for (Pair<? extends TKey, ? extends TValue> pair: pairs) {
        map.put(pair.getKey(), pair.getValue());
    }
    return map;
}

public static <TKey, TValue> HashMap<TKey, TValue> map(Pair<? extends TKey, ? extends TValue>... pairs) {
    return map(new HashMap<TKey, TValue>(), pairs);
}
Run Code Online (Sandbox Code Playgroud)

我没有测试过,但是试一试,看看你的表现如何.

PS,而不是使用化妆Pair类型,你可能会发现它更容易使用Map.Entry.