How can I create a Multimap in Java on Android

那年仲夏 提交于 2019-12-19 07:05:01

问题


Where can I find an implementation of multimap for Java that will work on Android without having to include any other classes etc. The implementations I've found all require other things which require other things and it gets messy quick. I'm porting a project from C++ and am new to java (and this project as well so I'm trying to keep things as identical as possible while getting it working) so any references or examples would be great.


回答1:


You could use a Map and a List to create a multimap. Say you wanted to associate an integer with a list of class type T. Use the following code:

Map<Integer, List<T>> myMultiMap = new HashMap<Integer, List<T>>();

It could get messy but, this should give you what you're looking for.

Another option is to use the Guava library's Multimap implementations.




回答2:


It looks like you are looking for Apache Commons Collection library. There you have a MultiMap class. I haven't tested it myself yet so I can't promise it works but it just looks about right for your project.

You won't find a MultiMap function in Java hence others have written libraries containing it. You can however try to implement it yourself if you're knowledge-level is high enough but that'd require some Java experience. So your best bet is to try to learn how to use and if necessary adapt libraries for using them on Android.




回答3:


If you care about code size, we just added multimaps to our utility library: http://greenrobot.org/2015/12/11/multimaps-in-greenrobot-common-2-2/

The jar is way below 100k, which is nice if you consider Android's 65K method limit.



来源:https://stackoverflow.com/questions/4109167/how-can-i-create-a-multimap-in-java-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!