With this instruction, Java initializes a new HashMap object with the attribute loadFactor at 0.75 and the DEFAULT_INITIAL_CAPACITY at 16. Many developers (me included) have the habit to declare a new Collection or Map using the no-argument constructor, e.g.: In Java is a good practice to initialize the initial capacity of collections and maps. Java will generate a Map like this: testMap => ) creates an Anonymous Inner Class, the second brace initializes a block inside the Anonymous Inner Class.įor more demanding implementation look at the documentation, there are other methods and different signatures in the Collectors class: toUnmodifiableMap, toConcurrentMap The ‘old’ classic methodįor more classical implementations you can initialize an HashMap in to steps, this implementation should be accessible to most of the developers: Map m圜lassicMap = new HashMap() īest practices: No argument constructor. Here the example if you have to add some elements: Map testMap = Map.of(1, "One", 2, "Two") Unmodifiable Map initialization with Map.of | at ImmutableCollections$AbstractImmutableMap.put (ImmutableCollections.java:1072) | at ImmutableCollections.uoe (ImmutableCollections.java:142) | unchecked call to put(K,V) as a member of the raw type In case someone tries to modify the object: emptyMap.put(1, "test") This method can be useful if you have to return an empty map: Map emptyMap = Map.of() Examples and use cases Empty unmodifiable Map You can read about the goals in the JEP 269. The goal of these methods is to 'reduce the verbosity' of Java when immutable collections are created. In the documentation you can find the characteristics of these maps. Map.of, Map.ofEntries and Map.copyOf create unmodifiable maps. Since Java 9 (so you are excused if you were using Java 8 until recently) there are some factory methods present in the Map class. We are then assigning the Map to a HashMap reference HashMap map = new HashMap(Ĭreate an unmodifiable Map using a factory method In the example below, we are using the of() method to create a Map of key-value pairs. You can use this method to create a Map of key-value pairs and then assign the Map to a HashMap reference. This allow you to initialize a Map inline. The of() method is a static method of the Map class in Java that returns a Map. We are then passing the List to the HashMap constructor to initialize the HashMap. In the example above, we are using the asList() method to create a List of key-value pairs. New AbstractMap.SimpleEntry("Banana", 2), You can use this method to create a List of key-value pairs and then pass the List to the HashMap constructor to initialize the HashMap. The asList() method is a static method of the Arrays class in Java that returns a List. The second HashMap, map2, is created using the constructor, and it contains the same key-value pairs as map1. The first HashMap, map1, is created using the put() method. In the example above, we are creating two HashMaps. The HashMap constructor takes another Map as a parameter and creates a new HashMap that contains the same key-value pairs as the original Map. Initialization using the ConstructorĪnother way to initialize a HashMap is to use the HashMap constructor. We are then using the put() method to add three key-value pairs to the map. In the example above, we are creating a HashMap with String as the key type and Integer as the value type. You can add multiple key-value pairs to the HashMap by calling the put() method multiple times. The put() method allows you to add key-value pairs to the HashMap. One of the simplest ways to initialize a HashMap is to use the put() method. Initialization Initialization using the put() Method
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |