-1

I'm currently confused as to when Map is better than HashMap or the other way around, and the same for list/arraylist...

Could someone please ELI5? I know how to use them but I need someone to clear it up for me when one should be used, thanks.

SJ19
  • 1,933
  • 6
  • 35
  • 68

2 Answers2

5

Map is data structure where you store key-value pairs, this structure allows you to quickly look up an object corresponding to given key. Example use case is dictionary - wherein, for a given word (key), you can lookup its meaning (value).

List is another type of data structure where you store list of objects. Unlike Map, you usually access objects in a list by iterating the array - you can also access an object if you knew at which position in the list the object is present.

In Java, Map and List are interfaces - they define common methods such data structures should have. One can choose to implement these interfaces in variety of ways based on various needs. HashMap is one such implementation of Map, whereas ArrayList is an implementation of List interface.

To find out what are the different implementations of Map and List, you can look at All Known Implementation Classes of Map and All Known Implementation Classes of List respectively in the Java Docs


When you decide to use a Map, you must pick a concrete implementation of Map interface that you will use to create Map objects - HashMap is commonly used implementation of Map interface (unless one has special needs in which case they can pick more apt implementation).

Similarly, When you decide to use a List, you must pick a concrete implementation of List interface that you will use to create List objects - ArrayList is commonly implementation of List interface (unless one has special needs in which case they can pick more apt implementation).


In Object Oriented Design, there is also a concept of Program to Interface. This suggests that one should declare their variables to be of type represented by an interface, rather than concrete implementation.

# This is preferred (return interface type)
public Map function() {
    return new HashMap();
}

# This should be avoided (do not return implementation type)
public HashMap function() {
    return new HashMap();
}

The reason for doing so is that if the person who developed the code for function later feels that the Map returned by function should have its keys ordered (as seen in real world Dictionary), then she may decide to use TreeMap (an implementation of Map in which keys are ordered) instead of HashMap.

When function is defined like public Map function(), then, all callers of function will remain unaware of this change and there will not be any change in their code. However, when function is defined like public HashMap function(), then, it will have to change to public TreeMap function(), and will force all users of function to change their code.

Program to Interface allows developers to keep the impact of the change in implementation choices to minimum.

In the same spirit, Java has a lot of interfaces (Map, Set, List, etc) and programmers should use them when they pass around the references to objects in function calls, whereas the code that instantiates concrete objects can pick one of the many implementations based on needs of the program.

Wand Maker
  • 18,476
  • 8
  • 53
  • 87
0

there is just a conceptual difference between them, Map is a interface or you can also consider it as parent class for all type of maps**(HashMap,TreeMap)** etc. and we can consider HashMap as concrete class which can be initiated. this same in case of list and array list List is a interface and ArrayList is a concrete class.

The advantage to using Map is that you can change the underlying object to be a different kind of map without breaking your contract with any code that's using it. If you declare it as HashMap, you have to change your contract if you want to change the underlying implementation.

Qasim Khokhar
  • 1,022
  • 2
  • 11
  • 32