How to add specific values from array list to the Hash map?

Sarah

Here is what I tried:

HashMap<String, List<Integer>> hmap = new HashMap<String, List<Integer>>();
List<Integer> valueList = new ArrayList<Integer>();

valueList.add(9);
valueList.add(8);
valueList.add(7);
valueList.add(6);
hmap.put("A", valueList);


valueList.add(5);
valueList.add(4);
valueList.add(3);
valueList.add(2);
hmap.put("B", valueList);


valueList.add(7);
valueList.add(6);
valueList.add(5);
 valueList.add(4);
hmap.put("C", valueList);


valueList.add(2);
valueList.add(3);
valueList.add(4);
valueList.add(5);
hmap.put("D", valueList);

When I try to retrieve the values of a specific key from hmap by doing this:

List<Integer> dimensions = hmap.get("A")

I am expecting the hash map to return [9,8,7,6] but instead it returns everything in the ArrayList i.e., [9,8,7,6,5,4,3,2,7,6,5,4,2,3,4,5]. How do I make the array list to push only the first values to that key in the HashMap? What am I missing?

I also tried valueList.clear() after each hmap.put() statement. That results in pushing only the last set of values i.e., [2,3,4,5] for all the keys in the HashMap.

Nikolas Charalambidis

It happens because you use the same valueList over again and again which continuously updates the values.

Put to each map entry a new separated list.

 // ...
 valueList.add(6);
 hmap.put("A", valueList);

 valueList = new ArrayList<Integer>();
 valueList.add(5);
 // ...
 valueList.add(2);
 hmap.put("B", valueList);

 valueList = new ArrayList<Integer>();
 valueList.add(7);
 // ... etc

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Java Hash map / Array List Count distinct values

分類Dev

In clojure enlive how to create template to add values and keys from a map

分類Dev

Remove specific values from an array

分類Dev

How to add values from a numeric list to a plot title?

分類Dev

Ruby - How to invert a Hash with an array values?

分類Dev

How to add additional values in to array

分類Dev

How to get the count of keys for values in a hash map using lambda

分類Dev

How do I use purrr::map with dataframe list to modify column values in specific dataframes without changing other dataframes in list?

分類Dev

Return specific values from array of hashes - JSON

分類Dev

Finding values from array in hash key and return counts of zero

分類Dev

How to update a map that has a list as values

分類Dev

How to create map with key and a list of values in JavaScript

分類Dev

How to get a specific list from a list of lists?

分類Dev

How to add an item to an array of array list

分類Dev

How to get a json object with specific key values, from a json array column?

分類Dev

How to import specific column from an excel sheet and store its values in a variable array in python?

分類Dev

Extract individual values from numpy array into a list

分類Dev

How to fetch specific data from hash if there are multiple matches for the key?

分類Dev

JAXB - how to map xml array to list of Objects

分類Dev

How to convert javascript array to specific list of objects

分類Dev

How to remove array items between specific values that contains specific phrase

分類Dev

Count duplicate values in hash of an array

分類Dev

How to add/remove from a firestore map field?

分類Dev

Java 1D array list collection .contains or Hash-Map.containsKey speed

分類Dev

Add a new column to a numpy array with values based on list of indices

分類Dev

How to Map Flutter JSON Strings from List?

分類Dev

combine df from list and average only on specific values

分類Dev

add a key value pair to hash in first array depending on the substring from second array

分類Dev

A method to remove duplicates from a multi dimensional array by comparing specific values

Related 関連記事

  1. 1

    Java Hash map / Array List Count distinct values

  2. 2

    In clojure enlive how to create template to add values and keys from a map

  3. 3

    Remove specific values from an array

  4. 4

    How to add values from a numeric list to a plot title?

  5. 5

    Ruby - How to invert a Hash with an array values?

  6. 6

    How to add additional values in to array

  7. 7

    How to get the count of keys for values in a hash map using lambda

  8. 8

    How do I use purrr::map with dataframe list to modify column values in specific dataframes without changing other dataframes in list?

  9. 9

    Return specific values from array of hashes - JSON

  10. 10

    Finding values from array in hash key and return counts of zero

  11. 11

    How to update a map that has a list as values

  12. 12

    How to create map with key and a list of values in JavaScript

  13. 13

    How to get a specific list from a list of lists?

  14. 14

    How to add an item to an array of array list

  15. 15

    How to get a json object with specific key values, from a json array column?

  16. 16

    How to import specific column from an excel sheet and store its values in a variable array in python?

  17. 17

    Extract individual values from numpy array into a list

  18. 18

    How to fetch specific data from hash if there are multiple matches for the key?

  19. 19

    JAXB - how to map xml array to list of Objects

  20. 20

    How to convert javascript array to specific list of objects

  21. 21

    How to remove array items between specific values that contains specific phrase

  22. 22

    Count duplicate values in hash of an array

  23. 23

    How to add/remove from a firestore map field?

  24. 24

    Java 1D array list collection .contains or Hash-Map.containsKey speed

  25. 25

    Add a new column to a numpy array with values based on list of indices

  26. 26

    How to Map Flutter JSON Strings from List?

  27. 27

    combine df from list and average only on specific values

  28. 28

    add a key value pair to hash in first array depending on the substring from second array

  29. 29

    A method to remove duplicates from a multi dimensional array by comparing specific values

ホットタグ

アーカイブ