Redian新闻
>
How to represent n:n relationship in Java?
avatar
How to represent n:n relationship in Java?# Java - 爪哇娇娃
g*a
1
I have two fields: X (string) and Y(integer).
One object of X can point to zero or multiple objects of Y and vise versa.
How to represent this n to n relationship between X and Y?
Any implementing class of Map in Java can do this job?
Thanks.
avatar
g*g
2
You better have a data structure of your own.
If n is not big or the relation is dense,
it's best to keep an n*m matrix.
Otherwise, you may have an array of objects X,
with every one instance keeping a vector pointing to Ys.
And vice versa for Y.

【在 g******a 的大作中提到】
: I have two fields: X (string) and Y(integer).
: One object of X can point to zero or multiple objects of Y and vise versa.
: How to represent this n to n relationship between X and Y?
: Any implementing class of Map in Java can do this job?
: Thanks.

avatar
g*a
3
The later method you said is what I do right now.
I would like to think out a way for easy search and traversing.
Any more suggestions? Thx.:)

【在 g*****g 的大作中提到】
: You better have a data structure of your own.
: If n is not big or the relation is dense,
: it's best to keep an n*m matrix.
: Otherwise, you may have an array of objects X,
: with every one instance keeping a vector pointing to Ys.
: And vice versa for Y.

avatar
n*e
5
If relationships are not intrinsic properties of X or Y, don't code them in X
or Y. Period. Imagine two scenarios. One is that you have another two
classes A and B and you want the same relationship between them. How would
you do? Code them again in A and B? The other is that what if the
relationship between X and Y change (say X is divorced with Y and get married
with Z). Modify X and Y? So don't code relationship in X or Y.
If you have a small number of Xs or Ys but dense relationships

【在 g******a 的大作中提到】
: The later method you said is what I do right now.
: I would like to think out a way for easy search and traversing.
: Any more suggestions? Thx.:)

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。