I'm trying to create a HashMap, loop through that HashMap, and finally, I'm trying create an ArrayList of key/value pairs -- from the HashMap -- that contain within its value set a parameter value. I realize that might be a little hard to understand, so I've provided my code to make sense of what I'm trying to accomplish.
package data; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class ProductInventory { private Map <String, ArrayList<String>> product; private ArrayList <String> buildProduct; private ArrayList <ArrayList<String>> addProduct; public ProductInventory() { /** Set default values **/ product = new <String, ArrayList<String>> HashMap(); buildProduct = new ArrayList<String> (); addProduct = new ArrayList<ArrayList <String>> (); /** START - Create ArrayList for each item and add to HashMap**/ ArrayList <String> c1 = new ArrayList<String>(); ArrayList <String> c2 = new ArrayList<String>(); 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 <String> tv1 = new ArrayList<String>(); ArrayList <String> tv2 = new ArrayList<String>(); 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 <String> a1 = new ArrayList<String>(); ArrayList <String> a2 = new ArrayList<String>(); 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 <String, ArrayList<String>> 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<String> 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**/ } } } } }
For the desired output we can assume that the productAttribute parameter value is "Computer".
Current output of the addProduct ArrayList:
Size of ArrayList: [2] Content of each element: [0] - (Size [10] [0] - "CMC769LLA" [1] - "Computer" [2] - "Apple" [3] - "iPad2" [4] - "499.00" [5] - "CUX31EDH72" [6] - "Computer" [7] - "Asus" [8] - "Zenbook" [9] - "1449.00" [1] - (Size [10]) /** It includes the exact same content as element [0] **/
Desired output of the addProduct ArrayList:
Size of ArrayList: [2] Content of each element: [0] - (Size [5]) [0] - "CMC769LLA" [1] - "Computer" [2] - "Apple" [3] - "iPad2" [4] - "499.00" [1] - (Size [5]) [0] - "CUX31EDH72" [1] - "Computer" [2] - "Asus" [3] - "Zenbook" [4] - "1449.00"
I can't seem to figure out what I'm doing wrong. Any help would be greatly appreciated.