6

我正在调用一个库,出于 API 兼容性的原因,它返回一个 type 的实例java.util.Dictionary,但我希望有一个实例实现java.util.Map.

如何轻松将java.util.Dictionary实例转换为java.util.Map实例?

4

4 回答 4

7

使用番石榴和 Java 8:

Iterator<String> keysIter = Iterators.forEnumeration(dict.keys());
Map<String, Object> dictCopy = Maps.toMap(keysIter, dict::get);

纯 Java 8 中的替代方案:

List<String> keys = Collections.list(dict.keys());
Map<String, Object> dictCopy = keys.stream()
           .collect(Collectors.toMap(Function.identity(), dict::get)); 
于 2015-10-15T18:51:06.303 回答
3

Dictionary这就是我可以自己编程此转换的方式(在具有泛型类型参数的附加假设下)。

添加方法

public static <K, V> Map<K, V> addDictionaryToMap(Dictionary<K, V> source, Map<K, V> sink) {
    for (Enumeration<K> keys = source.keys(); keys.hasMoreElements();) {
        K key = keys.nextElement();
        sink.put(key, source.get(key));
    }
    return sink;
}

例如用addDictionaryToMap(dict, new HashMap<String, String>()).

于 2012-10-30T14:57:31.597 回答
1

我更喜欢遵循一种public static <K, V> Map<K, V> valueOf(Dictionary<K, V> dictionary)命名这种方法的风格。我会要求番石榴将其添加到他们的地图课程中。

public static <K, V> Map<K, V> valueOf(Dictionary<K, V> dictionary) {
  if (dictionary == null) {
    return null;
  }
  Map<K, V> map = new HashMap<K, V>(dictionary.size());
  Enumeration<K> keys = dictionary.keys();
  while (keys.hasMoreElements()) {
    K key = keys.nextElement();
    map.put(key, dictionary.get(key));
  }
  return map;
}
于 2013-11-05T15:33:32.673 回答
1

一种有趣的实现方法是使用视图方法,它允许您将DictionaryMap链接在一起并且两者都具有完整功能。如以下代码示例所示:

Dictionary<String, String> dict = new Hashtable<>();

dict.put("PT", "Portugal");
dict.put("CH", "Switzerland");
System.out.println(dict);

Map<String, String> map = MapUtils.asMap(dict);
System.out.println(map);

map.remove("CH");
dict.put("UK", "United Kingdom");
System.out.println(dict);
System.out.println(map);

map.clear();
System.out.println(dict);
System.out.println(map);

实现将是这样的:

public class MapUtils {
    public static  <K, V> Map<K, V> asMap(Dictionary<K, V> dict) {
        return new AbstractMap<K, V>() {
            @Override
            public Set<Entry<K, V>> entrySet() {
                return new AbstractSet<Entry<K, V>>() {
                    @Override
                    public Iterator<Entry<K, V>> iterator() {
                        return new Iterator<Entry<K, V>>() {
                            private Enumeration<K> keys = dict.keys();
                            private Enumeration<V> elements = dict.elements();
                            private K k = null;

                            @Override
                            public boolean hasNext() {
                                return keys.hasMoreElements();
                            }

                            @Override
                            public Entry<K, V> next() {
                                k = keys.nextElement();
                                return new SimpleEntry<>(k,
                                        elements.nextElement());
                            }

                            @Override
                            public void remove() {
                                if (k == null) {
                                    throw new IllegalStateException();
                                }
                                dict.remove(k);
                                k = null;
                            }
                        };
                    }

                    @Override
                    public int size() {
                        return dict.size();
                    }
                };
            }

            @Override
            public int size() {
                return dict.size();
            }

            @Override
            public boolean containsKey(Object key) {
                return dict.get(key) != null;
            }

            @Override
            public V get(Object key) {
                return dict.get(key);
            }

            @Override
            public V put(K key, V value) {
                return dict.put(key, value);
            }

            @Override
            public V remove(Object key) {
                return dict.remove(key);
            }
        };
    }
}

一些被覆盖的方法是可选的,但出于性能原因我实现了它们。

于 2015-08-01T15:58:14.840 回答