Come aggiungere un elemento a un arraylist che si trova in una hashmap
Ci vuole una mappa che ha come chiave la parola chiave dai dati csv e come valore una lista che contiene tutti i valori alternativi corrispondenti a una parola chiave.
- Map> alternateMap = new HashMap<>();
Note that putting a value into a map with a key which is already present in that map will overwrite the previous value. So you have to put the list only the first time you find a new Keyword, i.e. when trying to add an alternative for a keyword, first check whether the corresponding List exists in the map, if not then create a List and put into the map, then add the Alternate to that list.
- while(...) {
- String keyword = ...;
- String alternate = ...;
- // check whether the list for keyword is present
- List alternateList = alternateMap.get(keyword);
- if(alternateList == null) {
- alternateList = new ArrayList<>();
- alternateMap.put(keyword, alternateList);
- }
- alternateList.add(alternate);
- }
- // printing the result
- for(Map.Entry> alternateEntry : alternateMap.entrySet()) {
- System.out.println(alternateEntry.getKey() + ": " +
- alternateEntry.getValue().toString());
- }
here ia complete code of mine i hope it will help you
package data;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class ProductInventory
{
private Map > product;
private ArrayList buildProduct;
private ArrayList > addProduct;
public ProductInventory()
{
/** Set default values **/
product = new > HashMap();
buildProduct = new ArrayList ();
addProduct = new ArrayList> ();
/** START - Create ArrayList for each item and add to HashMap**/
ArrayList c1 = new ArrayList();
ArrayList c2 = new ArrayList();
c1.add("computer"); c1.add("Apple"); c1.add("iPad2"); c1.add("499.00");
c2.add("computer"); c2.add("Asus"); c2.add("Zenbook"); c2.add("1449.00");
ArrayList tv1 = new ArrayList();
ArrayList tv2 = new ArrayList();
tv1.add("television"); tv1.add("Panasonic"); tv1.add("Viera"); tv1.add("899.00");
tv2.add("television"); tv2.add("Samsung"); tv2.add("Series 6"); tv2.add("1597.00");
ArrayList a1 = new ArrayList();
ArrayList a2 = new ArrayList();
a1.add("audio"); a1.add("Bose"); a1.add("321 GS Series III"); a1.add("999.00");
a2.add("audio"); a2.add("Onkyo"); a2.add("HT-S3400"); a2.add("329.00");
/** END - Create ArrayList for each item and add to HashMap **/
/** Add entrys to HashMap **/
product.put("CMC769LLA",c1);
product.put("CUX31EDH72",c2);
product.put("TVTCL50E3",tv1);
product.put("TVUN55D6000",tv2);
product.put("A321GSIIIBK",a1);
product.put("AHTS3400",a2);
}
/** For the sake of this example, the productAttribute parameter will be equal to "computer" **/
public void setProducts(String productAttribute)
{
for (Map.Entry > entry : product.entrySet()) /** Loop through all entrys in the HashMap **/
{
for (String s: entry.getValue()) /** Loop through all values in HashMap**/
{
/** Add product attributes **/
if (s.equals(productAttribute)) /** Check to see if param exists in the value set**/
{
/** Add key to buildProduct ArrayList **/
buildProduct.add(entry.getKey());
/*Loop through values of the corresponding key and add to buildProduct*/
for (Iterator i = entry.getValue().iterator(); i.hasNext();)
{
Object item = i.next();
buildProduct.add(item.toString());
}
/** Add buildProduct arrayList to product arrayList **/
addProduct.add(buildProduct);
break; /** Exit nested loop**/
}
}
}
}
}