New hashmap

A new HashMap!

In Java, a HashMap is a type of Map implementation that stores key-value pairs in a hash table. Here's a basic example of how to create a new HashMap:

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

public class Main {
    public static void main(String[] args) {
        // Create a new HashMap
        Map<String, Integer> myHashMap = new HashMap<>();

        // Add some key-value pairs
        myHashMap.put("John", 25);
        myHashMap.put("Jane", 30);
        myHashMap.put("Bob", 35);

        // Print the contents of the HashMap
        System.out.println(myHashMap);
    }
}

This will output:

{John=25, Jane=30, Bob=35}

Note that the order of the key-value pairs is not guaranteed, as HashMap uses a hash table to store the data.

You can also specify an initial capacity and load factor when creating a HashMap:

Map<String, Integer> myHashMap = new HashMap<>(16, 0.75f);

This sets the initial capacity to 16 and the load factor to 0.75, which means that the HashMap will start with 16 buckets and will resize itself when the load factor reaches 0.75 (i.e., when the number of key-value pairs is 75% of the initial capacity).

You can also use the putIfAbsent method to add a key-value pair only if the key is not already present in the map:

myHashMap.putIfAbsent("Alice", 28);

This will add the key-value pair "Alice" -> 28 to the map only if the key "Alice" is not already present.

And that's a basic overview of HashMap in Java!