initialize empty dictionary java

ht‮‬tps://www.lautturi.com
initialize empty dictionary java

To initialize an empty dictionary (also known as a map) in Java, you can use the HashMap class.

Here is an example of how to initialize an empty HashMap in Java:

Map<String, Integer> map = new HashMap<>();

This creates a new empty HashMap object with keys of type String and values of type Integer.

To add elements to the map, you can use the put() method:

map.put("apple", 1);
map.put("banana", 2);
map.put("cherry", 3);

In this example, the map variable is initialized with a new empty HashMap object, and the elements ("apple", 1), ("banana", 2), and ("cherry", 3) are added to the map using the put() method.

For more information on the Map interface and the HashMap class in Java, you can refer to the Java documentation.

Created Time:2017-11-01 22:29:52  Author:lautturi