Initialize a static map in Java with ExamplesIn Java, initializing a static map enables the creation of a map object associated with a class rather than an instance of the class. This allows the map to be shared among all instances of the class and accessed without creating an object. Static maps are particularly useful when storing data that needs to be shared across multiple instances of a class or maintaining a global map accessible throughout the application. Approach 1: Static Initialization Block
ALGORITHM:Step 1: Start the program execution. Step 2: Define a class named "StaticMapExample". Step 3: Inside the class, declare a private static map variable named "staticMap" with key as String and value as Integer. Step 4: Create a static initialization block using the "static" keyword. Step 5: Inside the static block: Step 5.1: Instantiate the static map using the "HashMap" class and assign it to the "staticMap" variable. Step 5.2: Populate the static map with key-value pairs: Step 5.2.1: Add the key "apple" with the value 10. Step 5.2.2: Add the key "banana" with the value 20. Step 5.2.3: Add the key "orange" with the value 30. Step 6: Define the main method to demonstrate accessing the static map. Step 7: Inside the main method: Step 7.1: Print the value associated with the key "apple" using the "get" method of the static map. Step 7.2: Print the value associated with the key "banana" using the "get" method of the static map. Step 8: Print the value associated with the key "orange" using the "get" method of the static map. Step 9: End the program execution. Implementation:The implementation of the above steps given below FileName: StaticMapExample.java Output: 10 20 30 Explanation: In the above code, The static map staticMap is defined as a class variable. The static initialization block is used to instantiate the map and populate it with key-value pairs. Inside the block, a new HashMap object is created, and the put method is used to add key-value pairs to the map. The main method is provided to demonstrate how to access and use the static map. Within the main method, the get method is used to retrieve and print the values associated with the key's "apple", "banana" and "orange" from the static map. Complexity Analysis:Time Complexity: Instantiating the HashMap and populating it with key-value pairs in the static initialization block has a time complexity of O (1) since the number of key-value pairs is fixed. Space Complexity: The space complexity of creating the static map in the static initialization block is O(n), where n is the number of key-value pairs in the map. Approach 2: Static Method
ALGORITHM:Step 1: Start the program execution. Step 2: Define a class named "StaticMapExample". Step 3: Inside the class, declare a private static map variable named "staticMap" with key as String and value as Integer. Step 4: Create a static method named "initializeStaticMap" that returns a map. Step 5: Inside the method: Step 5.1: Instantiate the static map using the "HashMap" class and assign it to the "staticMap" variable. Step 6: Populate the static map with key-value pairs: Step 6.1: Add the key "apple" with the value 10. Step 6.2: Add the key "banana" with the value 20. Step 6.3: Add the key "orange" with the value 30. Step 6.4: Return the initialized static map. Step 7: Define the main method to demonstrate accessing the static map. Step 8: Inside the main method: Step 8.1: Call the "initializeStaticMap" method to obtain the initialized static map and assign it to a map variable named "map". Step 8.2: Print the value associated with the key "apple" using the "get" method of the static map. Step 8.3: Print the value associated with the key "banana" using the "get" method of the static map. Step 8.4: Print the value associated with the key "orange" using the "get" method of the static map. Step 9: End the program execution. Implementation:The implementation of the above steps are given below FileName: StaticMapExample.java Output: 10 20 30 Explanation: In the given code, the static map staticMap is defined as a class variable. A static method named initializeStaticMap() is created to initialize and return the map. The map is instantiated using the HashMap class. Key-value pairs are added to the map using the put method. The initialized map is then returned. The main method is provided to demonstrate how to access and use the static map. The get method is used to retrieve and print the values associated with the keys "apple", "banana", and "orange" from the static map. Complexity Analysis:Time Complexity: Instantiating the HashMap and populating it with key-value pairs in the static method has a time complexity of O (1) since the number of key-value pairs is fixed. Space Complexity: The space complexity of creating the static map in the static method is O(n), where n is the number of key-value pairs in the map. Approach 3: Direct Initialization
ALGORITHM:Step 1: Start the program execution. Step 2: Define a class named "StaticMapExample". Step 3: Inside the class, declare a private static map variable named "staticMap" with key as String and value as Integer. Step 4: Initialize and populate the static map directly at the point of declaration using an anonymous inner class: Step 4.1: Instantiate the static map using the "HashMap" class. Step 4.2: Populate the static map with key-value pairs: Step 4.2.1: Add the key "apple" with the value 10. Step 4.2.2: Add the key "banana" with the value 20. Step 4.2.3: Add the key "orange" with the value 30. Step 5: Define the main method to demonstrate accessing the static map. Step 6: Inside the main method: Step 6.1: Access and use the static map. Step 6.2: Print the value associated with the key "apple" using the "get" method of the static map. Step 6.3: Print the value associated with the key "banana" using the "get" method of the static map. Step 6.4: Print the value associated with the key "orange" using the "get" method of the static map. Step 7: End the program execution. Implementation:The implementation of the above Steps given below FileName: StaticMapExample.java Output: 10 20 30 Explanation: The static map staticMap is defined as a class variable and is initialized directly at the point of declaration. An anonymous inner class is used, which extends HashMap to create an instance of the map. Inside the instance initializer block, key-value pairs are added to the map using the put method. The main method is provided to demonstrate how to access and use the static map. Within the main method, the get method is used to retrieve and print the values associated with the key's "apple", "banana", and "orange" from the static map. Complexity Analysis:Time Complexity: The initialization of the static map directly at the point of declaration has a time complexity of O (1). Space Complexity: The space complexity of the static map initialization is O(n), where n is the number of key-value pairs in the map. Next TopicJava APIs |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India