how do i import multimap for java?

13,768

Solution 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.

Solution 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

Share:
13,768
Matt M
Author by

Matt M

Updated on June 14, 2022

Comments

  • Matt M
    Matt M almost 2 years

    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

  • Matt M
    Matt M about 11 years
    I looked at that already and I still can't import it. I understand the premise of a multipmap, I just want the java file, thanks.
  • Cratylus
    Cratylus about 11 years
    What do you mean you can't import it?Are you using eclipse?Just add the jar in your build path.
  • Ar5hv1r
    Ar5hv1r about 11 years
    @Cratylus - I think that's what's confusing Matt, he's not used an external library before.
  • Ar5hv1r
    Ar5hv1r over 10 years
    From the Vector docs: "it is recommended to use ArrayList in place of Vector". More to the point however, I would go the opposite direction of "code around the issue" and advocate that any time you find yourself wanting the type signature you describe you should instead simply install Guava. Your code will be cleaner, you'll have fewer bugs, and everyone is happy.
  • Inversus
    Inversus over 10 years
    Your code may be cleaner, but it will also have more dependencies (ie Guava). "you'll have fewer bugs" is great general advice against the general advice to "code around the issue" but, in this situation, the example is so simple that there is very little risk of introducing bugs. In fact, I would even say that there is less risk than using a framework (such as Guava) that you don't really know much about. Good point about the vector, though. I've updated my answer accordingly.
  • Ar5hv1r
    Ar5hv1r over 10 years
    I would advocate that Guava is such a powerful and useful library that you're making a mistake working on a modern Java project without it; but I understand what you're saying, there's certainly a tradeoff between using a library and re-inventing the wheel.
  • Bala Vishnu
    Bala Vishnu about 10 years
    Thanks for the JAR download link :) I was looking for it