I found a thread on stackoverflow, but I'm just not tracking how to do this in my program.
I am reading the book "Job Ready Java" and one of the exercises is to create a program of student quiz scores that stores their name as a String and scores as an ArrayList in a Hashmap. I think what they want is for there to be a Hashmap like this:
StudentNam(key) : Scores(values)
Bob : [47, 59, 60, 27]
Frank : [89, 79, 80, 90]
I completely cannot fathom how to add multiple values into Scores using a HashMap. If it was just a HashMap<String, Integer> then I would do:
Hashmap<String, Integer> students = new HashMap<String, Integer>();
students.put("Bob" 69);But how to enter an array of values to represent Bob's scores if he has like 10 scores? TYIA and sorry if this is kind of stream of conscious question, but I'm racking my brain on this one.
java - How to add element into ArrayList in HashMap - Stack Overflow
java - How to add HashMap to ArrayList - Stack Overflow
java - Converting ArrayList to HashMap - Stack Overflow
java - Best way to create a hashmap of arraylist - Stack Overflow
I know, this is an old question. But just for the sake of completeness, the lambda version.
Map<String, List<Item>> items = new HashMap<>();
items.computeIfAbsent(key, k -> new ArrayList<>()).add(item);
HashMap<String, ArrayList<Item>> items = new HashMap<String, ArrayList<Item>>();
public synchronized void addToList(String mapKey, Item myItem) {
List<Item> itemsList = items.get(mapKey);
// if list does not exist create it
if(itemsList == null) {
itemsList = new ArrayList<Item>();
itemsList.add(myItem);
items.put(mapKey, itemsList);
} else {
// add if item is not already in list
if(!itemsList.contains(myItem)) itemsList.add(myItem);
}
}
problem:
prodHashMap.put("prod", tvProd.getText().toString());
You are using the same key each time you are adding an element to the the arraylist with the same reference to the HashMap thus changing its values.
Solution:
create a new instance of HashMap each time you want to add it to the ArrayList to avoid changing its values upon calling addProd
public void addProd(View ap)
{
// test arraylist of hashmaps
HashMap<String, String> prodHashMap = new HashMap<String, String>();
prodHashMap.put("prod", tvProd.getText().toString());
prodArrayList.add(prodHashMap);
tvProd.setText("");
// check data ///
Log.e("myLog","Data prodArrayList in ADD Method Size = "+prodArrayList.size());
for(int i=0; i< prodArrayList.size();i++)
{
Log.e("myLog","Data prodArrayList in ADD Method = "+prodArrayList.get(i).toString());
}
}
This is for adding multiple maps to List
Map<String,Object> map1=new HashMap<>();
map1. // add required items
Map<String,Object> map2=new HashMap<>();
map2. // add required items
Map<String,Object> map3=new HashMap<>();
map3. // add required items
List<String,Object> list=new ArrayList<>();
list.add(map1);
list.add(map2);
list.add(map3);
The general methodology would be to iterate through the ArrayList, and insert the values into the HashMap. An example is as follows:
HashMap<String, Product> productMap = new HashMap<String, Product>();
for (Product product : productList) {
productMap.put(product.getProductCode(), product);
}
Using a supposed name property as the map key:
for (Product p: productList) { s.put(p.getName(), p); }
You don't need to re-add the ArrayList back to your Map. If the ArrayList already exists then just add your value to it.
An improved implementation might look like:
Map<String, Collection<String>> map = new HashMap<String, Collection<String>>();
while processing each line:
String user = user field from line
String value = value field from line
Collection<String> values = map.get(user);
if (values==null) {
values = new ArrayList<String>();
map.put(user, values)
}
values.add(value);
Follow-up April 2014 - I wrote the original answer back in 2009 when my knowledge of Google Guava was limited. In light of all that Google Guava does, I now recommend using its Multimap instead of reinvent it.
Multimap<String, String> values = HashMultimap.create();
values.put("user1", "value1");
values.put("user2", "value2");
values.put("user3", "value3");
values.put("user1", "value4");
System.out.println(values.get("user1"));
System.out.println(values.get("user2"));
System.out.println(values.get("user3"));
Outputs:
[value4, value1]
[value2]
[value3]
Use Multimap from Google Collections. It allows multiple values for the same key
https://google.github.io/guava/releases/19.0/api/docs/com/google/common/collect/Multimap.html
You could either use the Google Guava library, which has implementations for Multi-Value-Maps (Apache Commons Collections has also implementations, but without generics).
However, if you don't want to use an external lib, then you would do something like this:
if (map.get(id) == null) { //gets the value for an id)
map.put(id, new ArrayList<String>()); //no ArrayList assigned, create new ArrayList
map.get(id).add(value); //adds value to list.
String courseID = "Comp-101";
List<String> scores = new ArrayList<String> ();
scores.add("100");
scores.add("90");
scores.add("80");
scores.add("97");
Map<String, ArrayList<String>> myMap = new HashMap<String, ArrayList<String>>();
myMap.put(courseID, scores);
Hope this helps!