I'm calling a library that, for API compatibility reasons, returns an instance of type java.util.Dictionary
, but I want to have an instance implementing java.util.Map
.
How can I easily convert an java.util.Dictionary
instance to a java.util.Map
instance?
Using Guava and Java 8:
Iterator<String> keysIter = Iterators.forEnumeration(dict.keys());
Map<String, Object> dictCopy = Maps.toMap(keysIter, dict::get);
Alternative in pure Java 8:
List<String> keys = Collections.list(dict.keys());
Map<String, Object> dictCopy = keys.stream()
.collect(Collectors.toMap(Function.identity(), dict::get));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With