用 Java 实现一对多映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6005617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Implementing a one to many map in Java
提问by algorithmicCoder
Have one object
of type A
that is related to a bunch of objects
of type B
and want to store all objects
of type A
and easily access their type B
relations.
有一个object
的type A
被涉及到一堆objects
的type B
,想存储所有objects
的type A
地访问他们的type B
关系。
What's the best (built-in?) data structure doing this in Java?
在 Java 中执行此操作的最佳(内置?)数据结构是什么?
回答by Zach L
You could have a Mapof type A
objects to a Listor Set(or whichever Collectionworks best) of type B
objects, like:
你可以有一个地图的type A
对象添加到列表或组(或任何类别的最好)的type B
对象,如:
Map<A,List<B>> map = new HashMap<A,List<B>>();
Or use Google's MultiMapinterface, which will do essentially the same as above, but with less work on your part.
或者使用 Google 的MultiMap界面,它的作用与上述基本相同,但您的工作较少。