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?
(That would be dependent on what keys you have)
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();
Client c = (Client)map.get("x");
String clientName = c.getName();
ASKER
while(true)
{
soc = ss1.accept();
ObjectInputStream ois = new ObjectInputStream(soc.getI nputStream ());
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 );
}
}
{
soc = ss1.accept();
ObjectInputStream ois = new ObjectInputStream(soc.getI
HashMap hm = (HashMap)ois.readObject();
// o = hm.get("crafter");
o = new crafter_class();
if(o == null)
{
System.out.println("Object
}
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
}
}
ASKER
i think i need to make Object o = cc;
Why would you be *creating* objects? You should be getting them as i mentioned
ASKER
basically i got the info stored in Object o. but i cant get do o.get_name(); (somehow)?
ASKER
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;
String name = cc.getName();
}
ASKER
crafter_class cc = (crafter_class)o;
i always f!@#ing forget that!!
thanks dude, it works fine now!!!! shot a kapillion!
i always f!@#ing forget that!!
thanks dude, it works fine now!!!! shot a kapillion!
ASKER
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 ;-)
ASKER
or how would i retrieve the String embeded in the HashMap?
ASKER
do you have an MSN or Yahoo chat address?
I don't use IM i'm afraid
ASKER
fine man.
so you know how to retrieve the origional String embeded in the HashMap?
so you know how to retrieve the origional String embeded in the HashMap?
What is its key?
ASKER
it is the key im looking for...
ie String s = hm.get(Object o);
=> s = "crafter"
ie String s = hm.get(Object o);
=> s = "crafter"
You can get all the keys thus:
Set keys = map.keySet();
Set keys = map.keySet();
ASKER
then?
basically i need to get a String from the HashMap (ie "crafter") to compare with others.
it may be "product", "sales-transaction" etc.
??
basically i need to get a String from the HashMap (ie "crafter") to compare with others.
it may be "product", "sales-transaction" etc.
??
ASKER
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);
}
}
Iterator iter = keys.iterator();
while (iter.hasNext()) {
Object o = iter.next();
if (o instanceof String) {
System.out.println(o);
}
}
probably you can check out this thread
https://www.experts-exchange.com/questions/21163672/How-do-I-sort-a-String-Array-based-on-a-key-of-substrings.html
https://www.experts-exchange.com/questions/21163672/How-do-I-sort-a-String-Array-based-on-a-key-of-substrings.html
ASKER
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?)
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;
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.
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.
ASKER
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!
and
String key = (String)o;
i always f!@#ing forget those!!
thanks dude, it works fine now!!!! shot a kapillion!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
String s = (String)map.get("x");