Results 1 to 1 of 1
  1. #1
    vb89 is offline Member
    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Default 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.

    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**/
                        }
                    }
                }  
        }
    }
    For the desired output we can assume that the productAttribute parameter value is "Computer".

    Current 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 **/
    Desired output of the addProduct ArrayList:
    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"
    I can't seem to figure out what I'm doing wrong. Any help would be greatly appreciated.
    Last edited by vb89; 02-27-2012 at 04:02 AM.

Similar Threads

  1. Looping issue
    By sehudson in forum New To Java
    Replies: 3
    Last Post: 07-11-2011, 06:05 PM
  2. Replies: 1
    Last Post: 04-05-2011, 08:58 PM
  3. Adding elements to an ArrayList
    By ArcherSam in forum Advanced Java
    Replies: 7
    Last Post: 01-28-2011, 03:05 PM
  4. Problem in adding arrayList elements
    By cool in forum New To Java
    Replies: 3
    Last Post: 01-03-2011, 12:27 PM
  5. Looping Array Elements
    By enzyme in forum New To Java
    Replies: 3
    Last Post: 11-26-2009, 04:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •