What Is a Java Map? - dummies

By John Paul Mueller

Arrays and specialized lists make it possible to perform an amazing array of tasks with Java. However, there are situations where a Java application needs something that’s more akin to a database, without actually having all the database baggage (such as having to buy a separate application).

For example, you might want to be able to find a specific value in a list without having to look at every element individually. The following sections describe a special sort of collection called a Map. The Map is an incredibly useful sort of storage container that makes many tasks easier.

Always remember that every time you add functionality to a class, it causes an increase in class size. In addition, the class runs more slowly and can become harder to understand. If an array will serve the purpose in your application, use an array. Only use a Map when you need the specialized functionality that a Map provides.

The Map, like many basic container classes in Java, is actually an interface. A Map describes a class that provides a key and value pair for storing information. The key gives the data a unique name. No two entries can have the same key, which makes it possible to search for the key and always return a unique value. Any number of keys can have the same associated value.

As with any interface, you must create an implementation before you can use it. The Map is so incredibly useful that Java has a host of implementing classes associated with it. Here’s the list of classes you can use to create a Map (again, you don’t need to worry about the specifics of these classes for now — you’ll begin to understand these differences as you create more Java applications):

  • AbstractMap

  • Attributes

  • AuthProvider

  • ConcurrentHashMap

  • ConcurrentSkipListMap

  • EnumMap

  • HashMap

  • Hashtable

  • IdentityHashMap

  • LinkedHashMap

  • PrinterStateReasons

  • Properties

  • Provider

  • RenderingHints

  • SimpleBindings

  • TabularDataSupport

  • TreeMap

  • UIDefaults

  • WeakHashMap

A single article can’t even begin to discuss all these classes. The important thing to remember is that all Map classes rely on a unique key to identify data and a value to hold the data.