Associative array like implementation in Java

Vaibhav Singh
Javarevisited
Published in
4 min readSep 15, 2020

Many programming languages support arrays with named indexes for e.g C++ , PHP , Python etc. Java has a round about implementation using Map data structure but its not an associative array.

Named associative key value arrays

What is Associative Arrays ?

Arrays with named indexes are called associative arrays (or hashes).

Associative Array in JavaScript

JavaScript does not support arrays with named indexes.In JavaScript, arrays always use numbered indexes.

Even JavaScript arrays are basically just objects, just with the special thing that the property names are numbers (0,1,…).

var myArray = {}; // creating a object
myArray['a'] = 20; // setting the attribute a to 20
myArray['b'] = 30; // setting the attribute b to 30

It’s important to understand that myArray['a'] = 20; is identical to myArray.a = 20;

Associative arrays in C++

Associative arrays are also called map or dictionaries. In C++. In these arrays indexing can be numeric or any other data type i.e can be numeric 0, 1, 2, 3.. OR character a, b, c, d…
These indexes are referred as keys and data stored at that position is called
value. So in associative array we have (key, value) pair.

Associative Arrays in PHP

There are two ways to create an associative array:

$age = array(“Naruto”=>”15", “Sasuke”=>”16", “Lee”=>”17");or$age[‘Naruto’] = “15”;
$age[‘Sasuke’] = “16”;
$age[‘Lee’] = “17”;

Associative Arrays in JAVA

Java like JavaScript does not support arrays with named indexes.Java arrays always use numbered indexes. But Java has an alternative dictionary implementation i.e Map which provides {key,value} pair Hash based mechanism with fast retrieval, collision handling mechanisms.

But since Map Interface has a lot of functionality to be implemented I decided to use the Dictionary interface which has pretty basic set of methods (though this interface is obsolete now , it is best suited for associative array named base indexes.

basic methods of Dictionary Interface

So Lets Dive in the Code:

I have created ArrayDictionary class by implementing the Dictionary Interface with two generic array variables of keys and values (K[] keys, V[] values), size & incrementalSize.

Below is the complete implementation github link:

Where do I use it ?

As explained in my blog on Serialization Techniques, developers who handle heavy scale tend to implement their own serialization mechanisms by choosing Externalizable/Writable interface and decide how to serialize/deserialize the member variable.

You can Read more on Problems with Java Serialization below :

Both the interface requires the developer to implement the read/write methods.

So many tend to use HashMap or LinkedHashMap implementations for writing to the stream and reading from the stream, just to avoid the byte read/write order.

For E.g Person class has id and name as its member variables. The implemented read and write methods look something like this (Below e.g is Externalizable read/write methods):

@Override
public void writeExternal(ObjectOutput out) throws IOException
{
Map<String, Object> serializedMap = new HashMap<>();
serializedMap.put("id", id);
serializedMap.put("name", this.getName());
out.writeObject(serializedMap);
}
@SuppressWarnings("unchecked")
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
{
Map<String, Object> deserializedMap = (Map<String, Object>) in.readObject();
this.id = (String) deserializedMap.get("id");
this.Name = (String) deserializedMap.get("name");
}

Hashmap provides the flexibility to not remember the order but adds an extra overhead of memory.

Thus to avoid I wrote a simpler and efficient memory implementation on the lines of associative array named indexes. So ArrayDictionary replaced the Hashmap, thus implementation would be if Writable interface was implemented :

public void write(DataOutput out) throws IOException 
{
Dictionary<String, Object> serializedMap = new ArrayDictionary<>();
serializedMap.put("id", id);
serializedMap.put("name", this.getName());
out.writeObject(serializedMap);
}

public void readFields(DataInput in) throws IOException
{
Dictionary<String, Object> deserializedMap = (ArrayDictionary<String, Object>) in.readObject();
this.id = (String) deserializedMap.get("id");
this.Name = (String) deserializedMap.get("name");
}

Thus above usage depicts the importance of named index base ArrayDictionary implementation.

If you like this blog content, please consider buying a coffee. Thank you for your support!

Questions ? Suggestions ? Comments ?

What’s next? Follow me on Medium to be the first to read my stories.

--

--

Vaibhav Singh
Javarevisited

I am a #TechnologyEnthusiast #Coder #JavaProgrammer #Blogger (https://linqz.io) #Dreamer. In my free time I love to #Cook, #ShareStories & #VolunteerForPoor.