#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0

    Trouble with method algorythm


    Hi Guys,

    Hoping someone can see where I messed up. I've created the test class below to which the output is:

    id: 40
    HID : 1
    NAME : sports
    id: 40
    HID : 4
    NAME : sewing
    temp.size(): 2
    HID/4
    NAME/sewing

    The final output should be:

    HID/1
    NAME/sports
    HID/4
    NAME/sewing

    This has me puzzled. temp.size() should be 4 NOT 2. It is clear from the println statements that my nested for loop in method getArrayFieldValues(...) is finding id 40 twice and putting the necessary values into the LinkedHashMap. But by the time it is done, it would seem the first two entries into the hashmap get overwritten or something leaving only the last two entries. I think this is one of those "can't see the forest for the trees" things. I need an extra pair of eyes to show me where I went wrong. Please advise.

    Alan
    Code:
    import java.util.*;   
      
    public class test   
    {   
      Field[] fields = null;   
         
      public static void main(String[] args)   
      {   
         test t = new test();   
         t.init();   
      }   
         
      private void init()   
      {   
         fields = new Field[2];   
         fields[0] = new Field("HID");   
         fields[1] = new Field("NAME");   
         
        String[][] data = new String[][]   
        {{"40","1","sports"},   
         {"41","1","sports"},   
         {"44","1","sports"},   
         {"39","2","crafts"},   
         {"38","3","photography"},   
         {"40","4","sewing"},   
         {"43","4","sewing"},   
         {"41","5","models"},   
         {"43","6","reading"}};   
            
         ArrayList<LinkedHashMap<String,Object>> hobbies = getArrayFieldValues("40",data);   
         LinkedHashMap<String,Object> temp = null;   
         for(int i = 0; i < hobbies.size(); i++)   
         {   
           temp = hobbies.get(i);   
           for (Map.Entry<String, Object> entry : temp.entrySet())   
           {   
             System.out.println(entry.getKey() + "/" + entry.getValue());   
           }   
              
         }      
      }   
         
      private ArrayList<LinkedHashMap<String,Object>> getArrayFieldValues(String id, String[][] data)   
      {   
             
          ArrayList<LinkedHashMap<String,Object>> list = new ArrayList<LinkedHashMap<String,Object>>();   
          int index = 0;   
          LinkedHashMap<String,Object> temp = new LinkedHashMap<String,Object>();   
          for(int i = 0; i < data.length; i++)   
          {   
              if(id.equals(data[i][0]))   
              {        
                  System.out.println("id: " + id);           
                  for(int j = 1; j < data[0].length; j++)   
                  {   
                    System.out.println(fields[index].getName() + " : " + data[i][j]);   
                    temp.put(fields[index].getName(),data[i][j]);   
                    index++;                   
                  }   
                  index = 0;//reset   
                     
              }   
          }   
          System.out.println("temp.size(): " + temp.size());   
          list.add(temp);   
          return list;   
      }   
    }   
      
    public class Field   
    {   
      private String name = "";   
      private String value = "";   
      
      public Field(String n)   
      {   
        name = n;   
      }   
      public Field(String n, String v)   
      {   
        name = n;   
        value = v;   
      }   
      
      public void setName(String value)   
      {   
        name = value;   
      }   
      
      public String getName()   
      {   
        return name;   
      }   
      
      public void setValue(String v)   
      {   
        value = v;   
      }   
      
      public String getValue()   
      {   
        return value;   
      }   
      
         
    }
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    Are you saying you are trying to add a key value pair to the map where the key is already in the map and the first value is replaced? This will necessarily happen.

    You can't have two key value pairs with the same key.

    If you have two key value pairs that you need to store in the map, you could make the value part an ArrayList and store the values there.
    Last edited by bullet; June 5th, 2013 at 05:10 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0
    You quite right. And after many hours of trying to figure this out, I finally came up with the solution. Since each hashmap can only have unique keys, I needed to create a new hashmap for every iteration. Final solution below:

    Code:
    private ArrayList<LinkedHashMap<String,Object>> getArrayFieldValues(String id, String[][] data)
      {
    	  
    	  ArrayList<LinkedHashMap<String,Object>> list = new ArrayList<LinkedHashMap<String,Object>>();
    	  int index = 0;
    	  LinkedHashMap<String,Object> temp = null; //new LinkedHashMap<String,Object>();
    	  for(int i = 0; i < data.length; i++)
    	  {
    		  if(id.equals(data[i][0]))
    		  {	
                  temp = new LinkedHashMap<String,Object>();		  
                  System.out.println("id: " + id);		  
    			  for(int j = 1; j < data[0].length; j++)
    			  {
    			    System.out.println(fields[index].getName() + " : " + data[i][j]);
    			    temp.put(fields[index].getName(),data[i][j]);
    			    index++;			    
    			  }
    			  index = 0;//reset
    			  list.add(temp);
    		  }
    	  }
    	  	  
    	  return list;
      }

IMN logo majestic logo threadwatch logo seochat tools logo