wood burning stoves*
The moose likes Java in General and the fly likes Declaring a MultiMap or BiMap; possible lesson to be learned? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Declaring a MultiMap or BiMap; possible lesson to be learned?" Watch "Declaring a MultiMap or BiMap; possible lesson to be learned?" New topic
Author

Declaring a MultiMap or BiMap; possible lesson to be learned?

Nikolas Bernadel
Greenhorn

Joined: Aug 09, 2012
Posts: 10

Hello,

I'm a little confused when it comes to declaring a MultiMap or BiMap, and this probably extends into a lesson that I may have to learn.

Currently, I'm declaring a MultiMap like so:

Now, I've been told that rather than use MultiMap, I should use its interfaces, which can be found here:

http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Multimap.html

The thing that I can't understand is what is the difference between this:

and this:

and this:


The same goes with the BiMap:

and this:

In the end I want an ArrayListMultimap and a HashBiMap. As I've been told, it's important to declare what you want, such as using a List or Set instead of a Collection.

Could someone provide an explanation? I'm still fairly new to Java it irks me that I don't know the correct declaration.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14116
    
  16

Welcome to the Ranch.

Your question is one that gets asked regularly. It applies to many other classes besides Multimap or BiMap; have a look at for example:

In general, the second approach is better. This has to do with the principles of information hiding and program to an interface, not an implementation.

Information hiding is about hiding implementation details of one part of the program from other parts. The less knowledge different parts of the program have of each other's implementation, the looser the coupling between the parts is, and the simpler and easier the program will become. It also makes it a lot easier to change the implementation of one part, without affecting other parts that use that part.

For example, most often when you have a list of things as above, the rest of the program only needs to know that it's a List<String> - it doesn't need to know what particular implementation of interface List you are using. Suppose that later on you find out that in this case a LinkedList would be a better choice than an ArrayList. If you declared the type of your variable as List<String>, you'd have to change only one line in your code. If it was ArrayList<String>, you have a problem, because you can't assign a LinkedList to a variable of type ArrayList.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Nikolas Bernadel
Greenhorn

Joined: Aug 09, 2012
Posts: 10

Jesper de Jong wrote:Welcome to the Ranch.

Your question is one that gets asked regularly. It applies to many other classes besides Multimap or BiMap; have a look at for example:

In general, the second approach is better. This has to do with the principles of information hiding and program to an interface, not an implementation.

Information hiding is about hiding implementation details of one part of the program from other parts. The less knowledge different parts of the program have of each other's implementation, the looser the coupling between the parts is, and the simpler and easier the program will become. It also makes it a lot easier to change the implementation of one part, without affecting other parts that use that part.

For example, most often when you have a list of things as above, the rest of the program only needs to know that it's a List<String> - it doesn't need to know what particular implementation of interface List you are using. Suppose that later on you find out that in this case a LinkedList would be a better choice than an ArrayList. If you declared the type of your variable as List<String>, you'd have to change only one line in your code. If it was ArrayList<String>, you have a problem, because you can't assign a LinkedList to a variable of type ArrayList.


Seems I've been doing it wrong for quite some time now. Just to make sure I have this down pat, the reason to use an interface rather than an implementation is strictly for flexibility. The only time to use an implementation is when you NEED that implementation.

Also, a big thanks for the quick reply and thorough explanation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Declaring a MultiMap or BiMap; possible lesson to be learned?