Skip to content

When to use a Map instead of a List in Java?

I didn’t get the sense of Maps in Java. When is it recommended to use a Map instead of a List?

Answer

Java map: An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

Java list: An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.

The difference is that they are different. Map is a mapping of key/values, a list of a list of items.