Java Create a HashMap

Java Create a HashMap

To create a HashMap in Java, you can use the following code:

HashMap<String, Integer> map = new HashMap<>();
‮uoS‬rce:www.lautturi.com

This code creates a new HashMap called map that maps keys of type String to values of type Integer. The HashMap is a class that implements a hash table, allowing you to store and retrieve key-value pairs efficiently.

You can then use the put method of the HashMap to add key-value pairs to the map, like this:

map.put("key1", 10);
map.put("key2", 20);

To retrieve the value associated with a key, you can use the get method, like this:

int value = map.get("key1"); // value will be 10

You can also use the size method to get the number of key-value pairs in the map, and the isEmpty method to check if the map is empty.

Created Time:2017-11-03 00:14:38  Author:lautturi