• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 363
  • Last Modified:

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?
0
minichicken
Asked:
minichicken
  • 15
  • 12
  • 2
1 Solution
 
CEHJCommented:
Object o = map.get("x");
String s = (String)map.get("x");
0
 
CEHJCommented:
(That would be dependent on what keys you have)
0
 
minichickenAuthor Commented:
how would i get the values stored in that object? (ie make Object o = new Cleint();)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CEHJCommented:
If Client objects are stored:

Client c = (Client)map.get("x");
String clientName = c.getName();
0
 
minichickenAuthor Commented:
           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);                  
                        }            
            }
0
 
minichickenAuthor Commented:
i think i need to make Object o = cc;
0
 
CEHJCommented:
Why would you be *creating* objects? You should be getting them as i mentioned
0
 
minichickenAuthor Commented:
basically i got the info stored in Object o.  but i cant get do o.get_name(); (somehow)?
0
 
minichickenAuthor Commented:
i can give you the entire calss if you'de dig to check it?
0
 
CEHJCommented:
if(o instanceof crafter_class)
{
    crafter_class cc = (crafter_class)o;
    String name = cc.getName();
}
0
 
minichickenAuthor Commented:
crafter_class cc = (crafter_class)o;

i always f!@#ing forget that!!

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

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

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

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

Set keys = map.keySet();
0
 
minichickenAuthor Commented:
then?

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

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

??
0
 
minichickenAuthor Commented:
how do i compare or view the keys?
0
 
CEHJCommented:
Set keys = map.keySet();
Iterator iter = keys.iterator();
while (iter.hasNext()) {
      Object o = iter.next();
      if (o instanceof String) {
            System.out.println(o);
      }
}      
0
 
kiranhkCommented:
0
 
minichickenAuthor Commented:
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?)
0
 
CEHJCommented:
1. 'keys' are the keys of the HashMap

2. An Iterator is to iterate the keys

3. String key = (String)o;
0
 
kiranhkCommented:
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.


0
 
minichickenAuthor Commented:
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!
0
 
CEHJCommented:
8-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 15
  • 12
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now