Comparison between ArrayList and HashMap

ArrayList and HashMap are commonly used containers in Java project development. Let’s compare the two!
Example:
//ArrayList

ArrayList array = new ArrayList();
array.add("张三");
array.add("李四");
array.add("王五");
System.out.println("ArrayList The number of elements is."+array.size());

//Iteration Method 1: Iteration via Iterator 
Iterator iter = array.iterator();  
while(iter.hasNext()){  
    String name = (String)iter.next();  
    System.out.println(name); 
}
//Iteration method 2: Iteration using a for loop
for(int i=0;i<array.size();i++){  
    System.out.println(array.get(i));  
}  

HashMap

HashMap hashMap = new HashMap();
hashMap.put("name", "张三");
hashMap.put("name1", "李四");
hashMap.put("name2", "王五");
System.out.println("HashMap的元素个数为:"+hashMap.size());

//Iteration method 1: the hashMap.entrySet() method, through the iterator Iterator iterate high efficiency, recommended to use
Iterator iter1 = hashMap.entrySet().iterator();  
while(iter1.hasNext()){  
    Map.Entry name = (Map.Entry)iter1.next();  
    String nameKey = (String)name.getKey();  
    String nameValue = (String)name.getValue();  
    System.out.println(nameKey + "'s name is " + nameValue);
}

//Iteration method 2: the hashMap.keySet() method, iterate through the Iterator Iterator is inefficient and not recommended.
Iterator iter2 = hashMap.keySet().iterator();
while (iter2.hasNext()) {
    Object key = iter.next();
    Object val = hashMap.get(key);
}

//Iteration method three: the foreach method to iterate over the keyset, no different from the second one.
Set keySet = hashMap.keySet();
for(Object key: keySet) {
    System.out.print("[key=" + key + ",value=" + hashMap.get(key) + "]  ");
}

//Iteration method 4: New method forEach in java 8.
hashMap.forEach((key,value) -> {
    System.out.print("[key=" + key + ",value=" + value + "]  ");
});

Similarities:
1) All threads are unsafe and out of sync
2) Both can store null values
3) The method of obtaining the number of elements is the same, and size() is used to obtain the number of elements
The difference between:
1) Implemented interface
ArrayList implements List interface (Collection (interface) -& GT; List (interface) -& GT; ArrayList (class), which USES arrays at the bottom; The HashMap is now the Map interface (Map (interface) -& GT; HashMap (class), which USES a Hash algorithm to store data.
2) Store elements
An ArrayList stores data as an array with sequential elements that can be repeated. A HashMap stores data as key-value pairs. The hashCode of the key cannot be the same. The same value will overwrite the previous value, the value can be repeated, and the elements inside are out of order.
3) Methods to add elements
ArrayList adds elements with the add(Object Object) method, while HashMap adds elements with the put(Object key, Object Value) method.
4) Default size and capacity expansion
In Java 7, the default size of an ArrayList is 10 elements, and the default size of a HashMap is 16 elements (which must be a power of 2).
//ArrayList source

 /**
  * Default initial capacity.
  */
 private static final int DEFAULT_CAPACITY = 10;

//a HashMap the source code

 /**
  * The default initial capacity - MUST be a power of two.
  */
 static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16

Expansion increment of ArrayList: 0.5 times +1 of the original capacity. For example, the capacity of ArrayList is 10, and the capacity after one expansion is 16.
HashMap expansion increment: one time of the original capacity, the loading factor is 0.75: that is, when the number of elements exceeds 0.75 times of the capacity length, the capacity is expanded. For example, the capacity of HashSet is 16, and after one expansion, the capacity is 32
Usage Scenario:
If you need quick random access to elements, you should use an ArrayList. When you need data in the form of key-value pairs, you should use a HashMap
Supplementary Content:
The load factor is the degree to which the elements in the Hash table are filled. If the loading factor is larger, the more elements are filled, the advantage is that the space utilization rate is higher, but the chance of conflict is increased. Conversely, the smaller the loading factor is, the fewer elements will be filled. The advantage is that the chance of conflict will be reduced, but more space will be wasted. The greater the chance of conflict, the higher the cost of finding. Conversely, the cost of finding is smaller. As a result, the search time is smaller. Therefore, a balance and compromise must be found between “opportunities for conflict” and “space utilization”. This trade-off is essentially a trade-off between the so-called “time-space” contradiction in data structures. Expansion occurs when the number of elements exceeds the coefficient of capacity length * loading factor.

if you have written wrong, I hope god more advice, let my younger brother correct, thank you!

Read More: