Link to home
Start Free TrialLog in
Avatar of minichicken
minichicken

asked on

hashmaps

does anyone know the code to retrieve the data stored in a hashmap (ie String, Object) and asign it to an Object and String?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Object o = map.get("x");
String s = (String)map.get("x");
(That would be dependent on what keys you have)
Avatar of minichicken
minichicken

ASKER

how would i get the values stored in that object? (ie make Object o = new Cleint();)
If Client objects are stored:

Client c = (Client)map.get("x");
String clientName = c.getName();
           while(true)
            {
                  soc = ss1.accept();
            
                  ObjectInputStream ois = new ObjectInputStream(soc.getInputStream());
                  
                  HashMap hm = (HashMap)ois.readObject();
                  
//                  o = hm.get("crafter");
                  
                  o = new crafter_class();
                                    
                  if(o == null)
                  {
                        System.out.println("Object does not Exist");
                  }
            
                  else
                        if(o instanceof crafter_class)
                        {
                              crafter_class cc = new crafter_class();
                              
                              String name = o.get_name();
                              String sname = o.get_sname();

                              crafter_container ccon = new crafter_container();

                              ccon.addcrafter(name,sname);                  
                        }            
            }
i think i need to make Object o = cc;
Why would you be *creating* objects? You should be getting them as i mentioned
basically i got the info stored in Object o.  but i cant get do o.get_name(); (somehow)?
i can give you the entire calss if you'de dig to check it?
if(o instanceof crafter_class)
{
    crafter_class cc = (crafter_class)o;
    String name = cc.getName();
}
crafter_class cc = (crafter_class)o;

i always f!@#ing forget that!!

thanks dude, it works fine now!!!! shot a kapillion!
but you say "String s = (String)map.get("x");" will get me the initial String decalred in the HashMap?
If there is a String in there, with "x" as a key, then yes ;-)
or how would i retrieve the String embeded in the HashMap?
do you have an MSN or Yahoo chat address?
I don't use IM i'm afraid
fine man.

so you know how to retrieve the origional String embeded in the HashMap?
What is its key?
it is the key im looking for...

ie String s = hm.get(Object o);

=> s = "crafter"
You can get all the keys thus:

Set keys = map.keySet();
then?

basically i need to get a String from the HashMap (ie "crafter") to compare with others.

it may be "product", "sales-transaction" etc.

??
how do i compare or view the keys?
Set keys = map.keySet();
Iterator iter = keys.iterator();
while (iter.hasNext()) {
      Object o = iter.next();
      if (o instanceof String) {
            System.out.println(o);
      }
}      
sorry dude,

it worked!  but

1. what is 'keys'?
2. what is 'Iterator' ?
3. how would i get String s = System.out.println(o); (if you know what i mean?)
1. 'keys' are the keys of the HashMap

2. An Iterator is to iterate the keys

3. String key = (String)o;
1. what is 'keys'?
keys is the variable name for your java collection Set Interface check out java.util.Set in the javadocs
2. what is 'Iterator' ?
Iterator is also the interface for moving over a collection of objects.  check out java.util.Iterator
3. how would i get String s = System.out.println(o); (if you know what i mean?)
System.out.pritnln(o) doesnt return you the string. basically what the code given above by CEHJ
means if the object O is an instance/type of java.lang.String then print the string.


crafter_class cc = (crafter_class)o;

and

String key = (String)o;

i always f!@#ing forget those!!

thanks dude, it works fine now!!!! shot a kapillion!
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial