bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths

Loading lesson path

Learn/Java/Java Reference
Java•Java Reference

Java HashMap get() Method

❮ HashMap Methods

Example

import java.util.HashMap;
public class Main {
  public static void main(String[] args) {
    HashMap<String, String> capitalCities = new HashMap<String, String>();
    capitalCities.put("England", "London");
    capitalCities.put("Germany", "Berlin");
    capitalCities.put("Norway", "Oslo");
    capitalCities.put("USA", "Washington DC");
    System.out.println(capitalCities.get("England"));
  }
}

Definition and Usage

The get() method returns the value of the entry in the map which has a specified key.

Syntax

public V get(Object
key )

V refers to the data type of the values of the map.

Parameter Values

ParameterDescription
keyRequired. Specifies the key of the entry to get the value from.

Technical Details

Returns:The value of the entry with the specified key or null if an entry with that key does not exist.

Previous

Java HashMap forEach() Method

Next

Java HashMap getOrDefault() Method