Home  >  Article  >  Java  >  Associative Array in Java

Associative Array in Java

王林
王林Original
2024-08-30 15:28:33637browse

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 Tests

For 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

How to Create an Associative Array in Java?

  • Java provides a Map class, or HashMap, which can be used as a type of array. The map Instead of referencing indexing (for example, 1, 2, 3, 4, 5, and so on), reference the objects of the array or reference the keys. So the map can be used as an alternative to the associative array.
  • You can use the put method to add an element to the array in a map. To access an element, the get method can be utilized. If you want to access all values of the array, the keySet function can be used. Also, we can remove elements from the map with the remove function and get the size of the array; the size method can be used (as the length function in arrays uses). So in simple words, a map associates (links) a value with a key.

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.

Examples to Implement Associative Array in Java

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:

Example #1

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:

Associative Array in Java

Example #2

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:

Associative Array in Java

Example #3

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:

Associative Array in Java

Example #4

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:

Associative Array in Java

Conclusion

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:String Array in JavaNext article:String Array in Java