I want to create a new map from the keyset()
of an existing HashMap
that maps a key to its index in the map.
Here's my attempt, which works:
Map<String, Integer> keysToIndex = new HashMap<>();
Integer index = 0;
for (String obj: hashMap.keySet()) {
keysToIndex.put(obj, index);
index += 1;
}
Does Java 8 provide features to create a map of each key's index, or is there a more idiomatic way to write this snippet?