mino
mino

Reputation: 7298

Java - Maps & HashMaps

So this is a very very basic question. I was reading though a class written by a colleague and I've only been doing Java for about six month and I cam across:

private Map<Dimension, Object> data = new HashMap<Dimension, Object>();

Of course, I consulted online but it didn't really give an explanation I could understand too well. So I'm wondering if anyone can explain what this code is doing and what Maps do in general? What is a Map or HashMap (and why when a Map is declared are they creating a HashMap?). Also what are Maps used for and what makes them better than say an ArrayList?

Upvotes: 2

Views: 409

Answers (4)

Mike Partridge
Mike Partridge

Reputation: 5281

From Oracle's Map Interface tutorial:

A Map is an object that maps keys to values. A map cannot contain duplicate keys: Each key can map to at most one value. It models the mathematical function abstraction.

So the contents of the Map in your code might be:

DimensionX => x-dimension stuff
DimensionY => y-dimension stuff
DimensionZ => z-dimension stuff

As to the difference between Maps and Arrays:

Maps store key/value pairs and provide accessors, e.g. to a value given a key.

Map<Dimension, Object> myDimensionMap = new HashMap<Dimension, Object>();
Dimension dimensionXKey = new Dimension("X");
Object dimensionXValue = myDimensionMap[dimensionXKey];

ArrayList (and Lists in general) provide ordered storage of values.

List<String> myTokenList = Arrays.asList(new String[]{"first", "second", "third"});
String firstToken = myTokenList.get(0); // "first"

Maps are great for caches, where you need to look something up based on a key; usually they're not ordered, but LinkedHashMap retains insertion order like a List.

The Map declaration in your code sample is pretty common, and it demonstrates the practice of declaring Collections variables as the interface type, and the value as an implementation of that interface. As others have pointed out, the interface (e.g. Map) defines the methods available to users, whereas the implementation (e.g. HashMap) implements logic to support the interface.

This is considered good practice because it allows you to change the underlying implementation of (in this case) Map without changing the code that uses it.

Upvotes: 3

Joe
Joe

Reputation: 47729

Map is the interface type. It defines how the class should behave. HashMap is the implementation. It is a way of implementing the Map behaviour, but it can be implemented in any number of ways (hash table, binary tree etc).

With that in mind, read these two documents

http://docs.oracle.com/javase/6/docs/api/java/util/Map.html

http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html

An ArrayList is another kind of collection (it conforms to the interface List). It just does different things (storing a list of objects rather than a mapping). The docs are quite clear.

http://docs.oracle.com/javase/6/docs/api/java/util/List.html

Upvotes: 5

Bernd Elkemann
Bernd Elkemann

Reputation: 23560

The difference to ArrayLists is that Maps store key->value associations while ArrayList's store indexable.

Your colleague can assign a HashMap to a variable typed Map because HashMap implements the Map interface ( http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html).

A reason why he might be doing this is to implicitly state that he is only using Methods declared in Map and does not depend on methods only implemented by HashMap

Upvotes: 1

Maps are used to store relationships. That is, you can associate a given key (and it can be something other than a number, as opposed to the keys on an array) with a value, and then efficiently retrieve it.

The Map used in the declaration is the interface for this type of collection, it only defines what any given Map should export to it's users, while the HashMap is an implementation of that Map interface which uses a Hash Table as the backing data structure.

You could make, for instance, an ArrayMap that implemented the Map interface but used an array behind it, although the data structure (array) wouldn't be very efficient for the operations described by the Map interface.

I suggest you read the docs for Map interface and HashMap implementation and also on the Hash Table concept on wikipedia.

Upvotes: 2

Related Questions