1

我想写一个可以用来初始化地图的方法。第一剪:

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

简单,但不是类型安全的。使用泛型,可能类似于:

<TKey, TValue> HashMap<TKey, TValue> map(TKey ... keys, TValue ... values) 

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

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;}
}

但是当我尝试它时,我需要转换它:

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> {}

谁能告诉我如何编写我的 map() 方法,使其完全通用但不需要强制转换?

4

3 回答 3

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);
}

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

PS,而不是使用虚构的Pair类型,您可能会发现它更易于使用Map.Entry

于 2008-11-28T11:17:58.903 回答
1

为什么不是这个?我是不是误会了什么?

import java.util.HashMap;
import java.util.Map;

public class ToHash {
    public static <K, V> Map<K, V> toHash(Object... objects) {
        Map<K, V> map = new HashMap<K, V>(objects.length / 2);
        if (objects.length % 2 != 0) {
            throw new IllegalArgumentException("Odd number of elements: " + objects.length);
        }
        for (int i = 0; i < objects.length; i += 2) {
            map.put((K) objects[i], (V) objects[i + 1]);
        }
        return map;
    }
}
于 2008-11-28T15:05:47.060 回答
0

pgdx:你的技术确实有效,但它并不妨碍我说类似的话:

Map<Long, Date> map = toHash("hello", "world");

我正在寻找一种允许编译器发现任何类型不匹配错误的方法。

于 2008-11-29T23:21:56.793 回答