Friday, January 17, 2025
HomeProgrammingHow do you create a dictionary in Java?

How do you create a dictionary in Java? [closed]

In Java, a dictionary is typically created using the Map interface, with the most common implementation being HashMap. Here’s how to create a dictionary in Java:

import java.util.HashMap;
import java.util.Map;

public class Main {
public static void main(String[] args) {
// Creating a dictionary (HashMap)
Map dictionary = new HashMap<>();

See also  How to Indent a Few Lines in Markdown Markup?

// Adding key-value pairs
dictionary.put(“apple”, 1);
dictionary.put(“banana”, 2);
dictionary.put(“cherry”, 3);

// Accessing values
System.out.println(dictionary.get(“apple”)); // Output: 1
}
}

In this example:

Map creates a dictionary where the keys are strings and the values are integers.

put() is used to insert key-value pairs.

See also  SQL where Datetime Column equals today's Date?

get() retrieves the value associated with a specific key.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x