What is the method of directly assigning values to initialize a hashmap?

In Java, you can directly initialize a HashMap and assign values by using the following method:

Map<String, Integer> map = new HashMap<String, Integer>() {{
    put("A", 1);
    put("B", 2);
    put("C", 3);
}};

In this method, double braces initialization block was used to directly assign values to HashMap. It is important to note that in this method, an anonymous inner class was created, which may lead to some performance issues.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds