Results 1 to 1 of 1
- 02-27-2012, 02:39 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 2
- Rep Power
- 0
Issue adding elements to an ArrayList while looping through a HashMap
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.
For the desired output we can assume that the productAttribute parameter value is "Computer".Java Code: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**/ { buildProduct.add(entry.getKey()); /** Add key to buildProduct ArrayList **/ /** 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**/ } } } } }
Current output of the addProduct ArrayList:
Desired output of the addProduct ArrayList:Java Code: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 **/
I can't seem to figure out what I'm doing wrong. Any help would be greatly appreciated.Java Code: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"Last edited by vb89; 02-27-2012 at 04:02 AM.
Similar Threads
-
Looping issue
By sehudson in forum New To JavaReplies: 3Last Post: 07-11-2011, 06:05 PM -
Adding all elements of an Array together, not adding up correctly
By Teclis in forum New To JavaReplies: 1Last Post: 04-05-2011, 08:58 PM -
Adding elements to an ArrayList
By ArcherSam in forum Advanced JavaReplies: 7Last Post: 01-28-2011, 03:05 PM -
Problem in adding arrayList elements
By cool in forum New To JavaReplies: 3Last Post: 01-03-2011, 12:27 PM -
Looping Array Elements
By enzyme in forum New To JavaReplies: 3Last Post: 11-26-2009, 04:35 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks