Reputation: 71
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.
Upvotes: 0
Views: 157
Reputation: 490
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);
}
}
Upvotes: 0
Reputation: 1
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);
}
Upvotes: 0
Reputation: 952
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);
}
Upvotes: 3