Solved

hashmaps

Posted on 2004-10-14
29
322 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
  • 15
  • 12
  • 2
29 Comments
 
LVL 86

Expert Comment

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

Expert Comment

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

Author Comment

by:minichicken
Comment Utility
how would i get the values stored in that object? (ie make Object o = new Cleint();)
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
If Client objects are stored:

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

Author Comment

by:minichicken
Comment Utility
           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
Comment Utility
i think i need to make Object o = cc;
0
 
LVL 86

Expert Comment

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

Author Comment

by:minichicken
Comment Utility
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
Comment Utility
i can give you the entire calss if you'de dig to check it?
0
 
LVL 86

Expert Comment

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

Author Comment

by:minichicken
Comment Utility
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
Comment Utility
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
Comment Utility
If there is a String in there, with "x" as a key, then yes ;-)
0
 
LVL 12

Author Comment

by:minichicken
Comment Utility
or how would i retrieve the String embeded in the HashMap?
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 12

Author Comment

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

Expert Comment

by:CEHJ
Comment Utility
I don't use IM i'm afraid
0
 
LVL 12

Author Comment

by:minichicken
Comment Utility
fine man.

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

Expert Comment

by:CEHJ
Comment Utility
What is its key?
0
 
LVL 12

Author Comment

by:minichicken
Comment Utility
it is the key im looking for...

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

=> s = "crafter"
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
You can get all the keys thus:

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

Author Comment

by:minichicken
Comment Utility
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
Comment Utility
how do i compare or view the keys?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
0
 
LVL 12

Author Comment

by:minichicken
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
8-)
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now