-
Notifications
You must be signed in to change notification settings - Fork 53.8k
[BAEL-9414] Add Values to Array List Used as Value in HashMap in Java #19146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+129
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3954a6d
[BAEL-9414] Add Values to Array List Used as Value in HashMap in Java
cc5dbea
[BAEL-9414] Add Values to Array List Used as Value in HashMap in Java…
0c6c08a
[BAEL-9414] Add Values to Array List Used as Value in HashMap in Java…
3a4028f
[BAEL-9414] Add Values to Array List Used as Value in HashMap in Java…
67ff51e
[BAEL-9414] Add Values to Array List Used as Value in HashMap in Java…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...lections-maps-9/src/main/java/com/baeldung/map/arraylistinhashmap/ArrayListInHashMap.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package com.baeldung.map.arraylistinhashmap; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.ArrayList; | ||
| import java.util.Map; | ||
| import java.util.List; | ||
| import java.util.Arrays; | ||
| import org.apache.commons.collections4.MultiValuedMap; | ||
| import org.apache.commons.collections4.multimap.ArrayListValuedHashMap; | ||
| import org.springframework.util.MultiValueMap; | ||
| import org.springframework.util.LinkedMultiValueMap; | ||
| import com.google.common.collect.Multimap; | ||
| import com.google.common.collect.ArrayListMultimap; | ||
|
|
||
| public class ArrayListInHashMap { | ||
|
|
||
| public static Map<String, List<String>> addKeyManually(Map<String, List<String>> map, String key, String value) { | ||
| if (!map.containsKey(key)) { | ||
| map.put(key, new ArrayList<String>()); | ||
| } | ||
| map.get(key).add(value); | ||
| return map; | ||
| } | ||
|
|
||
| public static Map<String, List<String>> addKeyWithComputeIfAbsent(Map<String, List<String>> map, String key, String value) { | ||
| map.computeIfAbsent(key, k -> new ArrayList<String>()).add(value); | ||
| return map; | ||
| } | ||
|
|
||
| public static MultiValuedMap<String, String> addKeyToApacheMultiValuedMap(MultiValuedMap<String, String> map, String key, String value) { | ||
| map.put(key, value); | ||
| return map; | ||
| } | ||
|
|
||
| public static MultiValueMap<String, String> addKeyToSpringLinkedMultiValueMap(MultiValueMap<String, String> map, String key, String value) { | ||
| map.add(key, value); | ||
| return map; | ||
| } | ||
|
|
||
| public static Multimap<String, String> addKeyToGuavaMultimap(Multimap<String, String> map, String key, String value) { | ||
| map.put(key, value); | ||
| return map; | ||
| } | ||
|
|
||
| } | ||
78 changes: 78 additions & 0 deletions
78
...-maps-9/src/test/java/com/baeldung/map/arraylistinhashmap/ArrayListInHashMapUnitTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| package com.baeldung.map.arraylistinhashmap; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.ArrayList; | ||
| import java.util.Map; | ||
| import java.util.List; | ||
| import java.util.Arrays; | ||
| import org.apache.commons.collections4.MultiValuedMap; | ||
| import org.apache.commons.collections4.multimap.ArrayListValuedHashMap; | ||
| import org.springframework.util.MultiValueMap; | ||
| import org.springframework.util.LinkedMultiValueMap; | ||
| import com.google.common.collect.Multimap; | ||
| import com.google.common.collect.ArrayListMultimap; | ||
|
|
||
| import org.junit.Test; | ||
| import java.util.Collection; | ||
| import static org.junit.Assert.assertNotNull; | ||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| class ArrayListInHashMapUnitTest { | ||
|
|
||
| private static final String K1 = "key1"; | ||
| private static final String K2 = "key2"; | ||
| private static final String K1_V1 = "key1_value1"; | ||
| private static final String K1_V2 = "key1_value2"; | ||
| private static final String K2_V1 = "key2_value1"; | ||
|
|
||
| private static void verifyMap(Map<String, ? extends Collection<String>> testMap) { | ||
| assertEquals(Map.of(K1, List.of(K1_V1, K1_V2), K2, List.of(K2_V1)), testMap); | ||
| } | ||
|
|
||
| @Test | ||
| void whenUsingAddKeyManually_thenMapMatches() { | ||
| Map<String, List<String>> map = new HashMap<>(); | ||
| ArrayListInHashMap.addKeyManually(map, K1, K1_V1); | ||
| ArrayListInHashMap.addKeyManually(map, K1, K1_V2); | ||
| ArrayListInHashMap.addKeyManually(map, K2, K2_V1); | ||
| verifyMap(map); | ||
| } | ||
|
|
||
| @Test | ||
| void whenUsingComputeIfAbsent_thenMapMatches() { | ||
| Map<String, List<String>> map = new HashMap<>(); | ||
| ArrayListInHashMap.addKeyWithComputeIfAbsent(map, K1, K1_V1); | ||
| ArrayListInHashMap.addKeyWithComputeIfAbsent(map, K1, K1_V2); | ||
| ArrayListInHashMap.addKeyWithComputeIfAbsent(map, K2, K2_V1); | ||
| verifyMap(map); | ||
| } | ||
|
|
||
| @Test | ||
| void whenUsingApacheMultiValuedMap_thenMapMatches() { | ||
| MultiValuedMap<String, String> map = new ArrayListValuedHashMap<>(); | ||
| ArrayListInHashMap.addKeyToApacheMultiValuedMap(map, K1, K1_V1); | ||
| ArrayListInHashMap.addKeyToApacheMultiValuedMap(map, K1, K1_V2); | ||
| ArrayListInHashMap.addKeyToApacheMultiValuedMap(map, K2, K2_V1); | ||
| verifyMap(map.asMap()); | ||
| } | ||
|
|
||
| @Test | ||
| void whenUsingSpringLinkedMultiValueMap_thenMapMatches() { | ||
| MultiValueMap<String, String> map = new LinkedMultiValueMap<>(); | ||
| ArrayListInHashMap.addKeyToSpringLinkedMultiValueMap(map, K1, K1_V1); | ||
| ArrayListInHashMap.addKeyToSpringLinkedMultiValueMap(map, K1, K1_V2); | ||
| ArrayListInHashMap.addKeyToSpringLinkedMultiValueMap(map, K2, K2_V1); | ||
| verifyMap(map); | ||
| } | ||
|
|
||
| @Test | ||
| void whenUsingGuavaMultimap_thenMapMatches() { | ||
| Multimap<String, String> map = ArrayListMultimap.create(); | ||
| ArrayListInHashMap.addKeyToGuavaMultimap(map, K1, K1_V1); | ||
| ArrayListInHashMap.addKeyToGuavaMultimap(map, K1, K1_V2); | ||
| ArrayListInHashMap.addKeyToGuavaMultimap(map, K2, K2_V1); | ||
| verifyMap(map.asMap()); | ||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.