0

Trying to add a collection of items into a hashmap of type String. I iterate over the collection and if the value isn't in the hashmap I add it to the map and if it is I increment the quantity by 1 instead of adding it again. Here is my code so far.

HashMap<String, ArrayList<Integer>> map = new HashMap<String, ArrayList<Integer>>();
Collection items1 = basket.getItems();
for (Iterator i = items1.iterator(); i.hasNext(); ) {
    Product p = (Product) i.next();
    if(map.containsKey(p.title)) {;
    }
    else {
        ArrayList<Integer> list = new ArrayList<Integer>();
        list.add(p.price);
        list.add(1);
        map.put(p.title,list);
    }
}

How would I access the stored integer in the ArrayList inside the HashMap and increment it by 1?

Any help would be greatly appreciated, thank you.

If I was trying to also then print that map in a table, how would I iterate through the map and print the correct values. So far I have this code:

    for (Map.Entry<String, String> entry : map.entrySet())
{   
System.out.println(entry.getKey() + "/" + entry.getValue());
ArrayList<Integer> l = map.get(p.title);
%>  

<tr>
    <td> <%out.println(map.getKey);%></td>
    <td> <%out.println(l.get(0);%></td>
    <td> <%out.println(l.get(1));%></td>
</tr>
<%      
}
%>

I have managed to get it to work when printing the collection but not with the map.

NLee57
  • 71
  • 1
  • 9
  • Getting a `p.title` would be a good start. But forget the map, for a second. How would you update the Arraylist, assuming you had it? In other words, you're asking two things, so only focus on one... – OneCricketeer Mar 20 '17 at 01:38

3 Answers3

3

You need to get the value of key p.title and add 1 to the list:

HashMap<String, ArrayList<Integer>> map = new HashMap<String, ArrayList<Integer>>();
    Collection items1 = basket.getItems();
    for (Iterator i = items1.iterator(); i.hasNext(); ) {
        Product p = (Product) i.next();

        if(map.containsKey(p.title)) {
            ArrayList<Integer> list = map.get(p.title);
            list.set(1, list.get(1) + 1);
            map.put(p.title, list);
        }
        else {
            ArrayList<Integer> list = new ArrayList<Integer>();
            list.add(p.price);
            list.add(1);
            map.put(p.title,list);
        }
    }

Maybe you can also:

HashMap<String, ArrayList<Integer>> map = new HashMap<String, ArrayList<Integer>>();
    Collection items1 = basket.getItems();
    for (Iterator i = items1.iterator(); i.hasNext(); ) {
        Product p = (Product) i.next();

        ArrayList<Integer> list = map.get(p.title);

        if (list == null) {
            list = new ArrayList<Integer>();
            list.add(p.price);
            list.add(1);
        } else {
            list.set(1, list.get(1) + 1);
        }
        map.put(p.title, list);
    }
Ke Li
  • 942
  • 6
  • 12
  • Thank you for your reply. if instead I wanted to add 1 to the 1 already in the list so it would be 2 not 1. How would I increment it by 1? That way the value inside the hashmap would be – NLee57 Mar 20 '17 at 01:41
  • I have ran into a new problem, any help would be appreciated, thank you. – NLee57 Mar 20 '17 at 02:26
  • You can iterate the map first and then iterate it's value. Here are some examples: http://stackoverflow.com/questions/2117557/how-to-iterate-an-arraylist-inside-a-hashmap-using-jstl/ – Ke Li Mar 20 '17 at 02:30
  • I have added my full code. Can you see any errors? I get a nullpointerexception. I can't for the life of me understand why it isn't working. – NLee57 Mar 20 '17 at 03:07
  • I suggest you put your java code in the backend java file instead of JSP file if possible. Here you can calculate the result (the map here) in java file, and only print the Map in JSP, that's would be much clearer. For NullPointException, you need to which line thrown this exception and make sure the null variable is initialized before accessing. I would also suggest you learn more about OOP, for example here, the ArrayList which the first element is product price and second one is product count, you can represent a new object. – Ke Li Mar 20 '17 at 03:45
0

this may help u,but i suggest u to use linkedList insead of arrayList

  if(map.containsKey(p.title)) {
  ArrayList<Integer> list=map.get(p.title);
  list.get(1)=list.get(1)+1;
  map.put(p.title,list);
}
songyang
  • 1
  • 1
0

I think this should do it.

You need to retrieve the ArrayList when you have a matching key in the map, and increment the integer in the second index in the list, because you've stored the Product price in the first index (index 0), it seems like you want to store the "count" in the second index (index 1).

HashMap<String, ArrayList<Integer>> map = new HashMap<String, ArrayList<Integer>>();
        Collection items1 = basket.getItems();
        for (Iterator i = items1.iterator(); i.hasNext(); ) {
            Product p = (Product) i.next();
            if(map.containsKey(p.title)) {
                // get the list of the existing product in the hash map
                ArrayList<Integer> listForProduct = map.get(p.title));

                // get one of the integers in the list (the integer at index one, because index zero is the price)
                int intInList = listForP.get(0);

                // increment this integer and put it back at index one in the list
                listForP.add(1, intInList + 1);
            }
            else {
                ArrayList<Integer> list = new ArrayList<Integer>();
                list.add(p.price);
                list.add(1);
                map.put(p.title,list);
            }
        }
Luke
  • 470
  • 6
  • 10