• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

HashMap with Class as value

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ...
 
Brendan Crisler
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carefully read Jaikirans post again. It's exactly what you need.
 
Brendan Crisler
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Brendan Crisler
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it!!! Thanks to everyone for their support.

B
 
Brendan Crisler
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's all working perfectly now!!! Thanks again for all the help.



 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic