Junior — Middle
How is the storage of multiple values associated with a single key implemented in the system?
sobes.tech AI
Answer from AI
To store multiple values associated with a single key in Java, collections are often used where the value is a list or a set. For example, Map<K, List<V>> or Map<K, Set<V>>. This allows one key to map to several values.
Example:
Map<String, List<String>> map = new HashMap<>();
map.put("key", new ArrayList<>(Arrays.asList("value1", "value2")));
// Adding a new value to an existing key
map.get("key").add("value3");