Reputation: 131
I want to store two values in hashMap's value field.
For example:
Key = 100, Value = 10, 20
I want to store in a way so that I could modify hashMap Value
value using key ?
Lets say If I want to modify 10 to 30 then entry in map would be
Key = 100, Value = 30, 20
Any way so that I could control both Value
values independently using key of hashmap ..?
Upvotes: 1
Views: 105
Reputation: 11483
I would also advocate using #computeIfAbsent
:
Map<Integer, List<Integer>> map = new HashMap<>();
List<Integer> values = map.computeIfAbsent(100, k -> new ArrayList<>());
values.add(10);
values.add(20);
//100 -> [10, 20]
You can abstract the #computeIfAbsent call behind a method to ensure you always have a valid list value.
Upvotes: 0
Reputation: 324
Map<Integer, Set<Integer>> dualValueMap = new HashMap<>();
Set<Integer> s1 = new HashSet<>();
s1.add(10);
s1.add(20);
dualValueMap.put(100, s1);
Upvotes: 1
Reputation: 3572
You could use com.google.common.collect.Multimap
Example:
Multimap<Integer, Integer> map = ArrayListMultimap.create();
map.put(100, 10);
map.put(100, 20);
Upvotes: 1