What is a Java map?

Java maps are a fundamental data structure for Java developers, helping you quickly store and retrieve information. But what is a Java map exactly, and how do Java maps work?

What are Java maps?

Java maps are an implementation of the map abstract data type for the Java programming language. Maps link keys with values: given a key, the map will return the associated value. Each key is unique, but the associated values may not be. The map abstract data type is one way to build a key-value store.

Maps in Java (and other programming languages) have a wide variety of use cases. Data structures such as lists are inefficient at checking whether an element is present in the list; in the worst case, you may have to look at every element in the entire list. On the other hand, maps have been specifically designed to prioritize the quick lookup of elements. This makes them ideal for situations in which lookup efficiency is a must, such as caches.

How do Java maps work?

Java maps are implemented by the java.util.Map interface. The most important methods of Java maps include:

  • clear(): Removes all elements from the map.
  • containsKey(): Returns a Boolean value based on whether the map contains the given key.
  • containsValue(): Returns a Boolean value based on whether the map contains the given value.
  • get(): Given a key in the map, returns the associated value.
  • isEmpty(): Returns a Boolean value based on whether the map is empty.
  • put(): Places the given key in the map and associates it with the given value.
  • remove(): Removes the given element from the map.
  • replace(): Replaces the existing entry associated with the given key, and associates the key with the new given value.

Because java.util.Map is only an interface, it cannot be instantiated directly in Java. Instead, users need to instantiate one of the classes that implement the Map interface, such as HashMap, LinkedHashMap, and TreeMap. The differences between these classes is as follows:

  • HashMap: HashMaps store the elements of the map in a hash table, which is a very efficient data structure for storing and retrieving elements. No matter how many elements are in the map, using a HashMap guarantees constant-time performance for the get() and put() methods.
  • LinkedHashMap: LinkedHashMaps are similar to HashMaps, but also store the elements of the hash table in a doubly linked list. The elements are usually sorted by the order in which they were inserted in the map. Using a LinkedHashMap gives users more control over the structure and ordering of the map.
  • TreeMap: TreeMaps store the elements of the map in a tree data structure: a single root node with “children” nodes that form their own subtrees. If possible, the elements of the map are sorted according to their natural ordering of the keys. For example, if the keys of the map are integers, then the TreeMap would build the tree in numeric order.

Java maps in Redis

Redis is an open-source, in-memory data structure store that is used to implement NoSQL key-value databases, caches, and message brokers. The Redis project includes several fundamental data types, such as strings, lists, sets, and sorted sets. Redis also includes the hash data type, which it defines as “a map between string fields and string values.”

While using Redis hashes does allow you to work with map data structures, it’s not the same as using Java maps in Redis. One problem is that Redis uses its own commands—such as HDEL, HEXISTS, and HGET—rather than the familiar methods for Java maps. Many Java developers find that there’s a significant learning curve when they start working with Redis.

For this reason, many Java developers choose to install a third-party Redis Java client such as Redisson. Redisson includes many implementations of familiar Java objects, collections, and constructs, including Java maps.

In Redisson, functionality for Java maps in Redis is implemented via the RMap interface, which reimplements the java.util.Map interface from standard Java.

Below is an example of how to use RMap in Redisson:

RMap<String, SomeObject> map = redisson.getMap("anyMap");
SomeObject prevObject = map.put("123", new SomeObject());
SomeObject currentObject = map.putIfAbsent("323", new SomeObject());
SomeObject obj = map.remove("123");

The RMap interface includes all of the familiar methods from Java maps. It also includes support for features such as local caches, data partitioning, and eviction policies.

Similar terms