Java - How to use HashMap and examples

HashMap is a type of Map and stores data consisting of key and value pairs.

HashMap has the following characteristics:

  • Allows both null keys and null values
  • Does not guarantee synchronization when accessing data internally
  • Does not guarantee the order of the data
  • Duplicate key values are not allowed, but duplicate values can be

HashMap provides the following APIs. Let`s see how to use it with an example.

  • put()
  • putAll()
  • get()
  • remove()
  • clear()
  • isEmpty()
  • keySet()
  • values()
  • containsKey()
  • containsValue()
  • replace()

put()

put() takes a key and a value as arguments. The passed argument is stored in the HashMap as a key-value relationship.

public V put(K key, V value)

The HashMap in the code below uses the key as a String and the value as an Integer. The data is saved with put(). Null is allowed for both key and value, and duplicate keys are updated with the last stored value.

Map<String, Integer> fruits = new HashMap<>();
fruits.put("apple", 1);
fruits.put("banana", 2);
fruits.put("kiwi", 3);
fruits.put(null, 4);
fruits.put("kiwi", 5);
System.out.println("fruits: " + fruits);

// fruits: {banana=2, null=4, apple=1, kiwi=5}

The execution result of the code is marked with comments.

HashMap.putAll()

putAll() saves all data for the Map passed as an argument.

public void putAll(Map<? extends K, ? extends V> m)

The following is an example of combining two Maps with putAll.

Map<String, Integer> fruits = new HashMap<>();
fruits.put("apple", 1);
fruits.put("banana", 2);
fruits.put("kiwi", 3);

Map<String, Integer> food = new HashMap<>();
food.put("coffee", 1);
food.put("hamburger", 2);
food.put("sandwich", 3);

food.putAll(fruits);
System.out.println("food: " + food);

// food: {banana=2, apple=1, kiwi=3, coffee=1, sandwich=3, hamburger=2}

get()

get() returns the value corresponding to the key passed as an argument. Returns null if key does not exist.

public V get(Object key) {

The following is an example of getting a value with get().

Map<String, Integer> fruits = new HashMap<>();
fruits.put("apple", 1);
fruits.put("banana", 2);
fruits.put("kiwi", 3);

System.out.println("get(apple): " + fruits.get("apple"));
System.out.println("get(kiwi): " + fruits.get("kiwi"));
System.out.println("get(undefined): " + fruits.get("undefined"));

// get(apple): 1
// get(kiwi): 3
// get(undefined): null

remove()

remove() deletes the data corresponding to the key passed as an argument. When deleted, value is returned. If the data does not exist, null is returned.

public V remove(Object key)

In the following example, delete some data and check the return value. Attempting to delete nonexistent data will return null.

Map<String, Integer> fruits = new HashMap<>();
fruits.put("apple", 1);
fruits.put("banana", 2);
fruits.put("kiwi", 3);

System.out.println("remove(apple): " + fruits.remove("apple"));
System.out.println("remove(kiwi): " + fruits.remove("kiwi"));
System.out.println("remove(undefined): " + fruits.remove("undefined"));
System.out.println("fruits: " + fruits);

// remove(apple): 1
// remove(kiwi): 3
// remove(undefined): null
// fruits: {banana=2}

clear(), isEmpty()

clear() clears all data in the HashMap.

public void clear()

isEmpty() returns true if the data in the HashMap is empty, otherwise false.

public boolean isEmpty()

If you look at the following example, you can see that the result of isEmpty() is different from the result of isEmpty() after deleting all data with clear() when there is data in the HashMap.

Map<String, Integer> fruits = new HashMap<>();
fruits.put("apple", 1);
fruits.put("banana", 2);
fruits.put("kiwi", 3);
System.out.println("fruits: " + fruits);
System.out.println("is empty? " + fruits.isEmpty());

fruits.clear();
System.out.println("fruits: " + fruits);
System.out.println("is empty? " + fruits.isEmpty());

// fruits: {banana=2, apple=1, kiwi=3}
// is empty? false
// fruits: {}
// is empty? true

keySet(), values()

keySet() returns the keys stored in the HashMap as a Set object.

public Set<K> keySet()

values() returns the values stored in the HashMap as a Collection object.

public Collection<V> values()

The following is an example of outputting each key and value.

Map<String, Integer> fruits = new HashMap<>();
fruits.put("apple", 1);
fruits.put("banana", 2);
fruits.put("kiwi", 3);
System.out.println("keySet(): " + fruits.keySet());
System.out.println("values(): " + fruits.values());

Set<String> keys = fruits.keySet();
for (String key : keys) {
    System.out.println("key: " + key);
}

Collection<Integer> values = fruits.values();
for (Integer value : values) {
    System.out.println("value: " + value);
}

// keySet(): [banana, apple, kiwi]
// values(): [2, 1, 3]
// key: banana
// key: apple
// key: kiwi
// value: 2
// value: 1
// value: 3

containsKey(), containsValue()

containsKey() returns true if the key passed as an argument to the HashMap exists, otherwise false.

public boolean containsKey(Object key)

containsValue() returns true if the key passed as an argument to the HashMap exists, otherwise false.

public boolean containsValue(Object value)

The following example is trying to check the key and value for the case of existence and nonexistence.

Map<String, Integer> fruits = new HashMap<>();
fruits.put("apple", 1);
fruits.put("banana", 2);
fruits.put("kiwi", 3);

System.out.println("containsKey(apple): " + fruits.containsKey("apple"));
System.out.println("containsKey(undefined): " + fruits.containsKey("undefined"));
System.out.println("containsValue(1): " + fruits.containsValue(1));
System.out.println("containsValue(0): " + fruits.containsValue(0));

// containsKey(apple): true
// containsKey(undefined): false
// containsValue(1): true
// containsValue(0): false

replace()

replace() replaces the value of the key passed as an argument with the value passed as an argument. The value to be replaced and deleted is returned. If a non-existent key is passed as an argument, null is returned.

public V replace(K key, V value)

The following is the code that attempts to replace an existing key and a non-existent key.

Map<String, Integer> fruits = new HashMap<>();
fruits.put("apple", 1);
fruits.put("banana", 2);
fruits.put("kiwi", 3);
System.out.println("fruits: " + fruits);

System.out.println("replace(apple, 10): "  + fruits.replace("apple", 10));
System.out.println("replace(undefined, 10): "  + fruits.replace("undefined", 10));
System.out.println("fruits: " + fruits);

// fruits: {banana=2, apple=1, kiwi=3}
// replace(apple, 10): 1
// replace(undefined, 10): null
// fruits: {banana=2, apple=10, kiwi=3}

There is also a replace() method with different arguments and return types. If you look at the argument, there are three. The first is the key, the second is oldValue, the third is newValue. Changes to newValue only when the value of the stored key is the same as oldValue. If it is replaced, it returns true, if not equal to oldValue, it is not replaced and false is returned.

public boolean replace(K key, V oldValue, V newValue)

The following is an example of a case where oldValue matches and is replaced, and a case where oldValue is different and not replaced.

Map<String, Integer> fruits = new HashMap<>();
fruits.put("apple", 1);
fruits.put("banana", 2);
fruits.put("kiwi", 3);
System.out.println("fruits: " + fruits);

System.out.println("replace(apple, 1, 10): "  + fruits.replace("apple", 1, 10));
System.out.println("replace(banana, 1, 10): "  + fruits.replace("banana", 1, 20));
System.out.println("fruits: " + fruits);

// fruits: {banana=2, apple=1, kiwi=3}
// replace(apple, 1, 10): true
// replace(banana, 1, 10): false
// fruits: {banana=2, apple=10, kiwi=3}

Reference

codechachaCopyright ©2019 codechacha