File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes HashMap with Class as value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "HashMap with Class as value" Watch "HashMap with Class as value" New topic
Author

HashMap with Class as value

Brendan Crisler
Greenhorn

Joined: Feb 07, 2007
Posts: 19
To anyone who will help me from killing myself over this:

I am having an issue using HashMap with a private class as the value for it. The private class is similar to a struct in C++ (map<string, struct> . It's made up of two parameters. Ex:

/*********************/

private class zType {
xType x;
long y;
}

private enum xType {
a,
b,
c,
}

HashMap map = new HashMap();
// wanted key = string and value = zType

/*************/

Now, when I create a variable of zType and try to give values to each parameter, and then try to add it to the HashMap, i get an error that the zType variable had not been initialized. Can someone help? I know it's not exactly a HashMap problem, but I would also like to know if this is even possible to do with a HashMap.

Thanks,
B
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9327
    
110

Without seeing the actual code, cant say much. But i can guess that you might be having code like:



If so then you will have to initialize the myVar first, like:



[My Blog] [JavaRanch Journal]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Can you show us a little more code and just where the error occurs? I'd expect to see you build a ZType object and put it in the map ...


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Brendan Crisler
Greenhorn

Joined: Feb 07, 2007
Posts: 19
I appreciate the help thus far. Here is the code (somewhat):

private enum xType
{
a,
b,
c,
d,
}

private static class zType
{
static xType x;
static long y;
}


private static HashMaptestMap = new HashMap();

public static void main(String[] args) {
zType z;

z.y = 23;
z.x = xType.a;

testMap.put("hello", z);
}

// I get the errors on the 'z' variables. For each use (z.y, z.x, and z within the add method, the error shows that it has not been initialized. But, if i initialize it to null in the beginning, I get a value of null when I print it out. See below:


public static void main(String[] args) {
zType z = null;
String var = "hello";

z.y = 23;
z.x = xType.a;

testMap.put(var, z);

System.out.println(testMap);
}

// Output - {hello = null}


This is where I get confused...

Thanks again,
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Carefully read Jaikirans post again. It's exactly what you need.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Brendan Crisler
Greenhorn

Joined: Feb 07, 2007
Posts: 19
Yeah, right before I got the reply, I figured it out. I can't believe that I didn't see it.

Now, this brings me to another issue....

I can't seem to cast from the iteration to my previous type. See below:

private enum xType
{
a,
b,
c,
d,
}

private static class zType
{
static xType x;
static long y;
}

private static Map testMap = new HashMap();

public static void main(String[] args) {
zType z = new zType();
zType w = new zType();
xType v;
long l;
String var = "hello";

z.y = 23;
z.x = xType.a;

testMap.put(var, trackDataType);

Set entries = testMap.entrySet();
Iterator it = entries.iterator();

if (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
w = (zType)entry; // It say illegal cast
v = z.x;
l = z.y;
}
}

Is there a way to make this work? It also says that I cannot cast from Map.Entry to zType. But, when I cast it (as shown), it says illegal cast.

Sorry to bother again, and thanks for the help.

B
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18120
    
  39

Is there a way to make this work? It also says that I cannot cast from Map.Entry to zType. But, when I cast it (as shown), it says illegal cast.


The Map.Entry instance, that is returned from the iterator, is an inner class that holds both the key / value pair for the entry. Assuming that the zType is the value of the pair, then you need to...

zType z = (zType) entry.getValue();

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Brendan Crisler
Greenhorn

Joined: Feb 07, 2007
Posts: 19
Got it!!! Thanks to everyone for their support.

B
Brendan Crisler
Greenhorn

Joined: Feb 07, 2007
Posts: 19
I do have another question:

I would like to pull data from the map based on a specific key. Basically, if I'm given a specific string (key), I want to look through the map for that specific string (key) and pull the values for it. How would I go about doing that?

Thanks in advance.
B

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18120
    
  39

Originally posted by Brendan Crisler:
I do have another question:

I would like to pull data from the map based on a specific key. Basically, if I'm given a specific string (key), I want to look through the map for that specific string (key) and pull the values for it. How would I go about doing that?


Here is the JavaDoc for the HashMap class. Do you see a method in the API that does *exactly* what you want?

Henry
Brendan Crisler
Greenhorn

Joined: Feb 07, 2007
Posts: 19
It's all working perfectly now!!! Thanks again for all the help.



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HashMap with Class as value
 
Similar Threads
is it worth adding a class to distinguish types
Problem with passing variables between methods and classes (I think)
Properties/Settings - a design question
HashMap and hashCode method
Problem running C++ implementaion in Netbeans 6.8 (cygwin compiler)