Difference hashmap and arraylist

4

Could anyone explain the difference between HashMap and ArrayList ?

    
asked by anonymous 05.06.2017 / 20:30

1 answer

5

ArrayList

is a set of elements of a defined type. It is an ordered structure of data, that is, the values can be accessed by their indexes.

Example:

ArrayList<string> lista = new ArrayList<>();
lista.add("Stack");
lista.add("Overflow");

This would be something like

Index | Elemento
  0   | "Stack"
  1   | "Overflow"

These elements can be accessed by their index

String str1 = lista.get(0); //str1 receberá "Stack"

HashMap

It is a set of key-value pairs, for each element (value) saved in a HashMap there must be a unique key attached to it. Elements in a HashMap should be accessed by your keys.

Example:

HashMap<Integer, String> hashMap = new HashMap<>();
hashMap.put(1, "Stack");
hashMap.put(5, "Overflow");

This would be something like:

Key | Value
 1  | "Stack"
 5  | "Overflow"

These elements can be accessed by the key

String str = map.get(5); //str receberá "Overflow"
    
05.06.2017 / 20:42