I am trying to create a Set of Strings which is filled with the keys from a Hashtable so a for-each loop can iterate through the Set and put defaults in a Hashtable. I am st
HashMap's keySet()
method already creates the set you need, so simply:
Set keys = defaults.keySet();
This is a view of the keys in defaults, so its contents will change when changes are made to the underlying (defaults
) map. Changes made to keys
will be reflected in the map, as well, but you can only remove...not add...keys from the map.
If you need a copy of the keys that doesn't interact with the original map, then use one of the types suggested, as in:
Set keys = new HashSet( defaults.keySet() );