I want to get rid of this second map

W

WP

Hello, I wan't to change something about my design, let me try and explain:
I'm developing some research tools for a university institution. It's my
first programming job, so be gentle, please. :)

During parsing of an XML schema file I build up a map. I have objects of
class A as the key and objects of class B as the value. Class B contains
objects of class C, but those objects can be null.
Sorry for the abstract names, but the real stuff in this research tool
is pretty abstract too, heh.
Here's my problem. Sometimes I have an object of class C (you know, that
is also contained by B) and from that I need to get class A (the key to
the map). How should I do this? A given object C will only occur once
among the B:s in the map. I've been maintaining a second map thus far
(key: class C, value: class A) but I want to get rid of the second map.

Any suggestions welcome!

- WP
 
P

Patricia Shanahan

WP wrote:
....
Here's my problem. Sometimes I have an object of class C (you know, that
is also contained by B) and from that I need to get class A (the key to
the map). How should I do this? A given object C will only occur once
among the B:s in the map. I've been maintaining a second map thus far
(key: class C, value: class A) but I want to get rid of the second map.
....

In that situation, I usually create a class that wraps the two maps up,
so that in the program logic I do a single put operation, but then have
two forms of get, one with an A as key, the other with a C as key. The
class is very simple - each instance delegates all its real work to two
fields, a Map<A,B> and a Map<C,A>.

The two maps still exist, but only one object knows about them.

Patricia
 
D

Daniel Pitts

WP said:
Hello, I wan't to change something about my design, let me try and explain:
I'm developing some research tools for a university institution. It's my
first programming job, so be gentle, please. :)

During parsing of an XML schema file I build up a map. I have objects of
class A as the key and objects of class B as the value. Class B contains
objects of class C, but those objects can be null.
Sorry for the abstract names, but the real stuff in this research tool
is pretty abstract too, heh.
Here's my problem. Sometimes I have an object of class C (you know, that
is also contained by B) and from that I need to get class A (the key to
the map). How should I do this? A given object C will only occur once
among the B:s in the map. I've been maintaining a second map thus far
(key: class C, value: class A) but I want to get rid of the second map.

Any suggestions welcome!

- WP

One way is to make your structure like this (and completely remove both
maps)

class B {
C c;
A a;
B(A a, C c) {
this.a = a;
this.c = c;
if (c != null) {
c.setA(a);
}
}
}

HTH,
Daniel.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top