Implementing Associate Array in Java
Last Updated :
27 Nov, 2020
An associative array stores the set of elements in the form of (key, value) pairs. An associative array is a collection of unique keys and collections of values where each key is associated with one value.
An associate array is an abstract datatype like a map that is composed of a (key, value) pair, such that each key-value appears at most once in the collection. Basically, an array with named indexes is known as an associative array or hashes.
In Java, it is difficult to form the associative array however this could easily be achieved using a HashMap:
Syntax:
Map<String, String> map = new HashMap<String, String>();
// method to add the key,value pair in hashmap
map.put("geeks", "course");
map.put("name", "geeks");
// method to get the value
map.get("name"); // returns "geeks"
Steps to implement the Associative array or associative the List Array using the Map Function :
1. First initialize the map
Map<String ,String> map = new HashMap<>();
2. Then Put the Key, Value to the map using put method
map.put("geeks","Course");
3. After putting all Key Value to the map Convert the map to set using the entrySet() Method
Set<Map.Entry<String ,String> > set = map.entrySet();
4. Then Now convert the set to the List Array using the function ;
List<Map.Entry<String ,String>> list=new ArrayList<>(set);
Implementation of associate array:
Java
// Java program to implement the associate array
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
// Forming the map
Map<String, String> map = new HashMap<>();
// method to store the value and
// key into the map
map.put("name", "rohit");
map.put("geeks", "course");
map.put("India Capital", "Delhi");
System.out.println(map.size());
Set<Map.Entry<String, String> > set
= map.entrySet();
List<Map.Entry<String, String> > list
= new ArrayList<>(set);
for (int i = 0; i < list.size(); i++)
{
System.out.println(list.get(i).getKey() + ": "
+ list.get(i).getValue());
}
}
}
Output3
India Capital: Delhi
geeks: course
name: rohit
- Time Complexity: O(n)
- Space Complexity: O(n)
We can iterate through the array using the iterator() method
Syntax:
Iterator it = map.entrySet().iterator();
Java
// Java program to implement the associate array
// and iterate it using iterator() method
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
// Forming the map
Map<String, Integer> map = new HashMap<>();
// method to store the put the value and
// key into the map
map.put("Roll no", 45);
map.put("Total Question", 113);
map.put("Marks ", 400);
// method to access the value based on
// the key
System.out.println(map.size());
Set<Map.Entry<String, Integer> > set
= map.entrySet();
List<Map.Entry<String, Integer> > list
= new ArrayList<>(set);
// using the iterator
Iterator it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
Output3
Total Question=113
Roll no=45
Marks =400
- Time Complexity: O(n)
- Space Complexity: O(n)
Similar Reads
Implementation of a Circular Resizable Array in Java
A Circular Resizable Array is a data structure that effectively maintains a fixed-size array by enabling members to be added or withdrawn circularly. It is sometimes referred to as a circular buffer or ring buffer. This cyclical behavior is especially helpful in situations when a smooth array wrap i
4 min read
Creating a Generic Array in Java
Arrays in Java are generated using a certain data type. On the other hand, you may create a generic array that functions with various object types by utilizing generics. You can build type-safe, reusable code by using generics. In this article, we will learn about creating generic arrays in Java.Jav
2 min read
Immutable Array in Java
In this article, we will talk about how to implement an immutable Array in Java. Immutable means we cannot modify it. If you have an immutable array in Java means refers to an array whose content and size cannot be changed after it has been initialized. How to Implement an Immutable Array in Java?So
2 min read
How to Add Element in Java ArrayList?
Java ArrayList class uses a dynamic array for storing the elements. It is like an array, but there is no size limit. We can add or remove elements anytime. So, it is much more flexible than the traditional array. Element can be added in Java ArrayList using add() method of java.util.ArrayList class.
2 min read
Dynamic Array in Java
Arrays are linear data structures, and similar types of elements will be inserted in continuous memory locations. Now as we know there is an issue with arrays that size needs to be specified at the time of declaration or taken from the user in Java. Hence, there arise dynamic arrays in Java in which
3 min read
Convert HashSet to array in Java
Java HashSet class is used to create a collection that uses a hash table for the storage of elements. It inherits AbstractSet class and implements Set Interface. The key points about HashSet are: HashSet contains unique elements only.HashSet allows null values.The insertion of elements in a HashSet
2 min read
How to Replace a Element in Java ArrayList?
To replace an element in Java ArrayList, set() method of java.util. An ArrayList class can be used. The set() method takes two parameters the indexes of the element that has to be replaced and the new element. The index of an ArrayList is zero-based. So, to replace the first element, 0 should be the
2 min read
Get Enumeration Over Java ArrayList
ArrayList class is a re-sizeable array that is present in java.util package. Unlike the built-in arrays, ArrayList can change their size dynamically where elements can be added and removed from an ArrayList. In java version 1, enum was not present. With advancement to version, 1.5 enum was introduce
3 min read
Java Program to Implement ArrayDeque API
The ArrayDeque in Java provides how to use resizable-array additionally to the implementation of the Deque interface. It is also referred to as Array Double Ended Queue or Array Deck. This is a special quite array that grows and allows users to feature or remove a component from each side of the que
3 min read
Deep Copy of 2D Array in Java
In Java, performing a deep copy of a 2D array containing objects requires careful consideration to ensure independence between the original and copied arrays. By creating new instances of objects for each element in the array, developers can achieve a true deep copy, preventing unintended side effec
3 min read