How can you create a hashmap object in Java?
In Java, HashMap objects can be created in two ways:
- Create an empty HashMap object using the default constructor of the HashMap class.
HashMap<String, Integer> hashMap = new HashMap<>();
- Create a HashMap object with an initial capacity using the constructor of the HashMap class.
HashMap<String, Integer> hashMap = new HashMap<>(10);
In the above code, String represents the type of key, Integer represents the type of value, and 10 represents the initial capacity.
Keep in mind that in Java, you can also use other constructors to create a HashMap object, such as specifying initial capacity and load factor. For specific constructors, please refer to the official Java documentation or view the source code of the HashMap class.