how do i import multimap for java?

老子叫甜甜 提交于 2019-12-02 10:17:42

问题


This is kind of stupid but how do I install MultiMap?

I need a way to store multiple values to keys and my map implementation isn't working


回答1:


That class, MultiMap, is not part of the Java standard library. It is part of Apache Commons, a separate set of utility classes many Java developers find useful. An alternate Multimap implementation (which I would recommend) is available in Guava, Google's utility library.

In either case, in order to use these classes, you need to download the jar distributed by the project, and add it to your classpath when you run your program. You can do this at the command line: Including jars in classpath on commandline (javac or apt) or in Eclipse: Adding a JAR to an Eclipse Java library

If you Google for phrases like "installing jars" and "adding jars to eclipse" you'll find many resources to help you if you're still struggling.




回答2:


A multimap is like a Map but it can map each key to multiple values. If your own does not work you can add apache commons collections to your classpath. Download the jar and include it in your classpath.

But you could also implement your own multimap as: HashMap<SomeObject, List<YourObject>>()
Check example here under multimaps




回答3:


It's not the MultiMap class, but I code around this issue by using a collection as the value thusly:

HashMap<String, ArrayList<Integer>> metrics = new HashMap<String, ArrayList<Integer>>();



回答4:


Maven dependency for multimap utility

 <dependency>
    <groupId>org.solovyev</groupId>
    <artifactId>common-collections-multimap</artifactId>
    <version>1.0.3</version>
</dependency>


来源:https://stackoverflow.com/questions/14925329/how-do-i-import-multimap-for-java

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