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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to map HashMap with Array List of Objects (list is the value in the hash map) into another HashMap using DozerBeanMapper?

How to return a list of keys from a Hash Map?

How can I split a delimited list and recombine the values into a hash map?

Firestore - how to add/subtract from a map of values

Python 3 How to add specific lines from a list to an array

Java Hash map / Array List Count distinct values

How to retrieve an array of values from a persistent map?

How to add className to list from map array depending on object value

Groovy: How to add a list as key and another list as values to a Map

How do I extract specific values from a DataFrame and add them to a list?

How to get values from a hash within an array

How to map an array to a hash

Loop specific values from deep nested hash and array

How to add values from Map<T,List<L>> map to List<L>?

How to add data to the array of hash map in the document using Firestore?

How to map values from an array to an array of Object?

How to add values of specific variables in a map

How to convert a list of Array Values into a Map in Dataweave

How to compare multiple arrays from dictionary values, and map per array element the dictionary key into new array/list

From array of objects, map values in specific order without repeating the order

Split String from list or hash map array list in Kotlin

Ruby on rails: store all specific key values from a hash into an array

How to add specific elements from list of lists?

How to add specific key to array values?

How to map data to a new array with specific fields and values and add a single object per day based on a range of dates

map an array found in a hash to individual hash values

How to echo all values from a specific array?

How to select specific number of values from array?

Add the sum of array values to hash (map / grep)