7 Replies Latest reply on Nov 10, 2010 10:13 PM by 811730

    Instances in map ( noob question )

    811730
      Afternoon all.

      I've added about 50 class instances to a map and im able to reference then and find names etc but i'm not able to use the object that a key represents.

      im able to loop through each object but i then want to be able to access a method for that instance, ie.

      Object metricBoxInstance = (Object)m.getKey();

      i then want to use metricBoxInstance.setText("for example");

      i know this will be simple but its my first time using map.

      all help appreciated and detail available if required.



      johnny

      Edited by: 808727 on Nov 9, 2010 5:14 AM
        • 1. Re: Instances in map ( noob question )
          tschodt
          808727 wrote:
          I've added about 50 class instances to a map and
          im able to reference then and find names etc but i'm not able to use the object that a key represents.

          im able to loop through each object
          Do you mean you are able to iterate through the map entrySet?
          Show us.
          but i then want to be able to access a method for that instance, ie.

          Object metricBoxInstance = (Object)m.getKey();

          i then want to use metricBoxInstance.setText("for example");
          • 3. Re: Instances in map ( noob question )
            811730
            Yeah tschodt, i meant Iterate.
            This is my code.
            mp = new HashMap<Object,String>(); //setup my map
            
            testbox = new metricBox(this,columnNames,data[0][i],config,columnNames[i]); //create instance of metricBox
            mp.put(testbox,columnNames[i]); //add to map

            Set s = mp.entrySet();
            Iterator it = s.iterator();

            while(it.hasNext()){

            Map.Entry m = (Map.Entry)it.next();
            Object metricBoxInstance =(Object)m.getKey();
            String value = (String)m.getValue();

            ** at this point i wanna send a set text method to each metricBox but dont know how.
            }
            thanks                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
            • 4. Re: Instances in map ( noob question )
              jtahlborn
              metricBox metricBoxInstance =(metricBox)m.getKey();
              • 5. Re: Instances in map ( noob question )
                PhHein
                Why not
                mp = new HashMap<metricBox,String>();
                in the first place?
                • 6. Re: Instances in map ( noob question )
                  811730
                  superb, thanks.

                  Its simple now i see it.

                  johnny
                  • 7. Re: Instances in map ( noob question )
                    811730
                    I had tried that originally but it wouldn't compile.
                    So you didn't try that, you tried an incorrect version of it. You should perserve beyond a mere compile error especially when trying the correct technique.