gpt-4-0314· OpenAI
WildChat-4.8M (AI2)
Hi write an java code to create new hashmap
- User
Hi write an java code to create new hashmap
- Assistant
Here's the Java code to create a new HashMap:
javaimport java.util.HashMap; import java.util.Map; public class HashMapExample { public static void main(String[] args) { // Creating a new HashMap Map<String, Integer> hashMap = new HashMap<>(); // Adding key-value pairs to the HashMap hashMap.put("one", 1); hashMap.put("two", 2); hashMap.put("three", 3); // Printing the HashMap System.out.println("HashMap: " + hashMap); } }In this code, a new HashMap named
hashMapis created which holds keys of type String and values of type Integer. Then, it adds a few key-value pairs (like "one", 1) to the HashMap and finally prints the HashMap.