An associative array stores the set of elements in the form of (key, value ) pairs. An associative array comprises unique keys and collections of values, associating each key with a single value. The associative array data structure in java benefits a wide range of applications. Like Perl and PHP (using hash variables), other programming languages implement the functionality to work with the associative array data structures. As the associative array stores elements in the form of (key, value) pair, to access the element from the associative array, we need to call the name of an array and passkey(whose value we want to access).
ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock TestsFor example, an array(name as marks) stores students’ roll no and marks. To access the mark of a particular student, we should call this marks[102], where marks are an array name, and 102 is a rollno of a student, not an index number, which is not possible in an array of java. Therefore associative array technically not support in java, but it can be achieved using in the form of instances of the java.util.HashMap class.
Syntax:
Map <String, String> map = new HashMap <String, String>( ); // creates Map where keys and values of string type //method to store elements map.put( "key1", "value1" ); map.put( "key2", "value2" ); // etc // method to access value map.get( "key1" ); // returns "value1" as output
Advantages of Associative Array
With the associative array, we can assign the meaningful key to values for array elements, save more elements, and assign the string as a key to an array’s elements.
We understand the above methods with the below sample java code. To create the map, we need to import the utilities that allow the use of the map. So we will import the Map and HashMap utilities. Below are examples of implementing an Associative Array in Java:
Traverse Associative Array various methods
Code:
import java.util.HashMap; public class Demo { public static void main(String[] args ) { HashMap <String, String> capitals = new HashMap <String, String>(); capitals.put("Spain", "Madrid"); capitals.put("United Kingdom", "London"); capitals.put("India", "Delhi"); capitals.put("Argentina", "Buenos Aires"); System.out.println("The Size of capitals Map is : " + capitals.size()); // Remove an element from the HashMap capitals.remove("United Kingdom"); // To display size of the hashtmap System.out.println("The Size of capitals Map is : " + capitals.size()); // Check the existence of key in the Hashmap String key = "India"; if (capitals.containsKey( key )) { System.out.println("The capital of " + key + " is: " + capitals.get( key )); } else { System.out.println("There is no entry for the capital of " + key); } } }
Output:
Traverse Associative Array using iterator methods
Code:
import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class DurationClassDemo { public static void main(String[] args) { HashMap<String, String> capitals = new HashMap<String, String>(); capitals.put("Spain", "Madrid"); capitals.put("United Kingdom", "London"); capitals.put("India", "Delhi"); capitals.put("Argentina", "Buenos Aires"); System.out.println("The Size of capitals Map is : " + capitals.size()); Iterator i = capitals.entrySet().iterator(); // Iterate through the hashmap while (i.hasNext()) { Map.Entry ele = (Map.Entry)i.next(); System.out.println(ele.getKey() + " : " + ele.getValue()); } } }
Output:
Traverse Associative Array using a for-each loop
Code:
import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class DurationClassDemo { public static void main(String[] args) { HashMap<String, String> capitals = new HashMap<String, String>(); capitals.put("Spain", "Madrid"); capitals.put("United Kingdom", "London"); capitals.put("India", "Delhi"); capitals.put("Argentina", "Buenos Aires"); System.out.println("The Size of capitals Map is : " + capitals.size()); for (Map.Entry ele : capitals.entrySet()) { String key = (String)ele.getKey(); System.out.println(key + " : " + ele.getValue()); } } }
Output:
Traverse Associative Array using the forEach( ) method of hashmap
Code:
import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class DurationClassDemo { public static void main(String[] args) { HashMap<String, String> capitals = new HashMap<String, String>(); capitals.put("Spain", "Madrid"); capitals.put("United Kingdom", "London"); capitals.put("India", "Delhi"); capitals.put("Argentina", "Buenos Aires"); System.out.println("The Size of capitals Map is : " + capitals.size()); capitals.forEach((k, v) -> System.out.println(k + " : " + v )); } }
Output:
In simple words, an associative array in java stores the set of elements in a key; the value pair form an associative array is a collection of unique keys and collections of values where each key is associated with one value. We can use the hashMap built-in java class to achieve an associative array, as we have seen above examples.
The above is the detailed content of Associative Array in Java. For more information, please follow other related articles on the PHP Chinese website!