Hi, I'm making an app that uses Collections.
Everything went ok until now, what I've done:
I've got a class that initializes 3 strings, those strings are private, so I have made 6 public functions that sets or reads values from those strings.

Next I've made a hashmap, keys are generated as strings made by merging values from those 3 strings of a class and the values are objects of the first class.

Now'm stuck when I've got to iterate/access to those 3 Strings by reading them from map.
By entryset() and values() I could get what? I need access to those public functions that reads values of class Strings separately and set their values to other Strings.

I can't make it so:

class Objects {
String string1;
String string2; 
String string3;


public getString1(){
return string1;
}
//etc....


}

...

class showAll {

Set entries = data.entrySet(); //where data is actual hashmap
Iterator it = entries.iterator();

while(it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
String surname = iterator.next().getString1(); //IT DOESN'T WORK, how to achieve this?
System.out.println(surname);
}
}

...

It's not an actual code, it's just a draft.

Please for help.

Recommended Answers

All 4 Replies

Are you saying you have a class that you want to place into a Hashtable, the retrieve elements of that from the Hash?

Yup, exactly.

OK. Let's say you have a class called CPerson that has some functions -- one of which is called getLName tha returns the last name. The properties are the first name, the last name and the age:

Here is some code that will load an array of CPerson objects into a Hashtable then allow you to query for them; calling the written-in accessors.

import   java.util.*;

public class DW_393019
{
   ////////////////////////////////////////////////////////////////////////////
   // Hash
   private  static Hashtable<String, CPerson> m_map;

   ////////////////////////////////////////////////////////////////////////////
   //
   public static boolean LoadPeople(Object strError)
   {
      boolean blnRetVal = true;
      CPerson[]   arrPeople =
      {
         new CPerson("Tom",         "Jones", 43),
         new   CPerson("Julie",      "Jackson", 42),
         new CPerson("Hannah",      "Jarvis", 14)
      };

      m_map =  new Hashtable<String, CPerson>(arrPeople.length);

      try
      {
         for(CPerson p : arrPeople)
         {
            m_map.put(p.getFName(), p);
         }

      }
      catch(NullPointerException npe)
      {
         blnRetVal = false;
         strError = npe.getMessage();
      }

      return blnRetVal;
   }

   ////////////////////////////////////////////////////////////////////////////
   //
   public static CPerson FindPerson(String strPerson)
   {
      if(!m_map.containsKey(strPerson))
      {
         return (new CPerson("Not-Found", "Not-Found", 0));
      }

      return m_map.get(strPerson);
   }

   ////////////////////////////////////////////////////////////////////////////
   //
   public static void main(String[] args)
   {
      String strError = "";
      if(!LoadPeople(strError))
      {
         System.out.println("Could not load people: " + strError);
         return;
      }

      System.out.println(FindPerson("Tom").getLName());
      System.out.println(FindPerson("Hannah").getLName());
      System.out.println(FindPerson("Julie").getLName());
      System.out.println(FindPerson("Angus").getLName());
   }
}

thines01 Thank you very much for your help :) Problem solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.