[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

hashmaps

Posted on 2004-10-14
29
Medium Priority
?
362 Views
Last Modified: 2010-03-31
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
Comment
Question by:minichicken
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 15
  • 12
  • 2
29 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12311371
Object o = map.get("x");
String s = (String)map.get("x");
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12311375
(That would be dependent on what keys you have)
0
 
LVL 12

Author Comment

by:minichicken
ID: 12311420
how would i get the values stored in that object? (ie make Object o = new Cleint();)
0
Independent Software Vendors: 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!

 
LVL 86

Expert Comment

by:CEHJ
ID: 12311458
If Client objects are stored:

Client c = (Client)map.get("x");
String clientName = c.getName();
0
 
LVL 12

Author Comment

by:minichicken
ID: 12311483
           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
 
LVL 12

Author Comment

by:minichicken
ID: 12311496
i think i need to make Object o = cc;
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12311505
Why would you be *creating* objects? You should be getting them as i mentioned
0
 
LVL 12

Author Comment

by:minichicken
ID: 12311553
basically i got the info stored in Object o.  but i cant get do o.get_name(); (somehow)?
0
 
LVL 12

Author Comment

by:minichicken
ID: 12311559
i can give you the entire calss if you'de dig to check it?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12311583
if(o instanceof crafter_class)
{
    crafter_class cc = (crafter_class)o;
    String name = cc.getName();
}
0
 
LVL 12

Author Comment

by:minichicken
ID: 12311624
crafter_class cc = (crafter_class)o;

i always f!@#ing forget that!!

thanks dude, it works fine now!!!! shot a kapillion!
0
 
LVL 12

Author Comment

by:minichicken
ID: 12311638
but you say "String s = (String)map.get("x");" will get me the initial String decalred in the HashMap?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12311654
If there is a String in there, with "x" as a key, then yes ;-)
0
 
LVL 12

Author Comment

by:minichicken
ID: 12311660
or how would i retrieve the String embeded in the HashMap?
0
 
LVL 12

Author Comment

by:minichicken
ID: 12311669
do you have an MSN or Yahoo chat address?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12311684
I don't use IM i'm afraid
0
 
LVL 12

Author Comment

by:minichicken
ID: 12311701
fine man.

so you know how to retrieve the origional String embeded in the HashMap?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12311712
What is its key?
0
 
LVL 12

Author Comment

by:minichicken
ID: 12311742
it is the key im looking for...

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

=> s = "crafter"
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12311764
You can get all the keys thus:

Set keys = map.keySet();
0
 
LVL 12

Author Comment

by:minichicken
ID: 12311813
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
 
LVL 12

Author Comment

by:minichicken
ID: 12311832
how do i compare or view the keys?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12311895
Set keys = map.keySet();
Iterator iter = keys.iterator();
while (iter.hasNext()) {
      Object o = iter.next();
      if (o instanceof String) {
            System.out.println(o);
      }
}      
0
 
LVL 8

Expert Comment

by:kiranhk
ID: 12311916
0
 
LVL 12

Author Comment

by:minichicken
ID: 12311965
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12311998
1. 'keys' are the keys of the HashMap

2. An Iterator is to iterate the keys

3. String key = (String)o;
0
 
LVL 8

Expert Comment

by:kiranhk
ID: 12312041
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
 
LVL 12

Author Comment

by:minichicken
ID: 12312054
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 12312099
8-)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question