Tuesday, 3 January 2012

Java: The Map Interface

Hi again! I'll be going through the Map interface today in a little more detail, just so you know how it works. It's pretty cool. If you've done other programming, you might know this interface as a dictionary, which is what I likened it to before.


As I've mentioned before, the map is an object that maps keys to values, much like a dictionary would map words to meanings. Because of this likeness, a map cannot contain duplicate keys.

Unfortunately, each key can only map to at most 1 value, but you can work around this by making the value a list to contain all values. Silly, and where our dictionary likeness falls apart, but only a little. Here's a screenshot of a class that makes use of the map interface:


There's nothing in there, since the Map's an interface, but the method names should give you an idea of how one functions. Standard "size' and 'isEmpty' methods shouldn't give you any trouble. The 'contains....' methods should also be self-explanatory; a boolean saying whether the map contains the given key, or mapped value.

So on and so forth! It's basically a nice way to hold things and their attributes nicely. In our case, we've held players and their hand values as values and keys!

So there you have it. A neat little interface that does sweet backflips or something, I don't know. See you tomorrow for the start of our ConsoleClient!

3 comments: