Skip to content

Commit c2a88dc

Browse files
author
Deepak Malik
committed
Associative Array
1 parent d07add2 commit c2a88dc

File tree

6 files changed

+224
-5
lines changed

6 files changed

+224
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Below topics/problems are covered as of now.
3636
**2. Arrays**
3737
- [X] [Arrays Introduction](../master/src/com/deepak/data/structures/Arrays/Arrays_Introduction.md)
3838
- [X] [Basic operations on Arrays](../master/src/com/deepak/data/structures/Arrays/BasicOperations.java)
39-
- [ ] Associative Array
39+
- [X] [Associative Array](../master/src/com/deepak/data/structures/Arrays/AssociativeArray.java)
4040
- [ ] Sparse Array
4141
- [ ] Tuple
4242
- [X] [ArrayList Implementation](../master/src/com/deepak/data/structures/Arrays/CustomArrayList.java)
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/**
2+
* Data-Structures-In-Java
3+
* AssociativeArray.java
4+
*/
5+
package com.deepak.data.structures.Arrays;
6+
7+
/**
8+
* Associative Arrays in Java :
9+
*
10+
* Associative arrays are data structure, where keys are associated with some sort
11+
* of values. Though java does not support associative arrays, but same functionality
12+
* can be achieved through HashMap or HashTable.
13+
* Below is the replica of HashMap just names as associative arrays.
14+
*
15+
* @author Deepak
16+
*/
17+
public class AssociativeArray<K, V> {
18+
19+
/* Table to hold key value pairs */
20+
private Entry<K, V>[] table;
21+
22+
/* Variable to hold size of the map */
23+
private int size;
24+
25+
/* Default table size */
26+
private static final int DEFAULT_TABLE_SIZE = 10;
27+
28+
/**
29+
* Constructor
30+
*/
31+
public AssociativeArray() {
32+
this(DEFAULT_TABLE_SIZE);
33+
}
34+
35+
/**
36+
* Constructor
37+
*
38+
* @param size
39+
*/
40+
@SuppressWarnings("unchecked")
41+
public AssociativeArray(int size) {
42+
this.table = new Entry[size];
43+
this.size = 0;
44+
}
45+
46+
/**
47+
* Method to add/update a entry
48+
*
49+
* @param key
50+
* @param value
51+
*/
52+
public void put(K key, V value) {
53+
boolean isNewEntry = true;
54+
/* Find the hash of the key and bucket it belongs to */
55+
int hash = key.hashCode();
56+
int bucket = getBucket(hash);
57+
/* Loop through each entry of the associative array
58+
* and check if it's just a value update or a new key, value */
59+
for (Entry<K, V> entry : table) {
60+
if (entry != null && entry.getKey().equals(key)) {
61+
entry.value = value;
62+
isNewEntry = false;
63+
}
64+
}
65+
/* Create a new entry and push to array */
66+
if (isNewEntry) {
67+
Entry<K, V> newEntry = new Entry<>(key, value, hash);
68+
newEntry.next = table[bucket];
69+
table[bucket] = newEntry;
70+
size++;
71+
}
72+
}
73+
74+
/**
75+
* Method to find the value for a given key
76+
*
77+
* @param key
78+
* @return {@link V}
79+
*/
80+
public V get(K key) {
81+
/* Find hash code and bucket associated with key */
82+
int hash = key.hashCode();
83+
int bucket = getBucket(hash);
84+
/* Get all the entries on that column of bucket */
85+
Entry<K, V> entry = table[bucket];
86+
while (entry != null) {
87+
/* If hash and key matches, return the value */
88+
if (entry.getHash() == hash && entry.getKey() == key) {
89+
return entry.getValue();
90+
}
91+
entry = entry.next;
92+
}
93+
return null;
94+
}
95+
96+
/**
97+
* Method to get size of associative array
98+
*
99+
* @return {@link int}
100+
*/
101+
public int size() {
102+
return size;
103+
}
104+
105+
/**
106+
* Method to check if associative array is empty
107+
*
108+
* @return {@link boolean}
109+
*/
110+
public boolean isEmpty() {
111+
return size == 0;
112+
}
113+
114+
/**
115+
* Method to get bucket based on the hash
116+
*
117+
* @param hash
118+
* @return {@link int}
119+
*/
120+
private int getBucket(int hash) {
121+
return (hash % table.length);
122+
}
123+
124+
/**
125+
* Static class Entry for associative array
126+
*
127+
* @author Deepak
128+
*
129+
* @param <K>
130+
* @param <V>
131+
*/
132+
public static class Entry<K, V> {
133+
134+
/* Variables for key, value, hash and next entry */
135+
private final K key;
136+
private V value;
137+
private Entry<K, V> next;
138+
private int hash;
139+
140+
/**
141+
* Constructor
142+
*
143+
* @param key
144+
* @param value
145+
* @param hash
146+
*/
147+
public Entry(K key, V value, int hash) {
148+
this.key = key;
149+
this.value = value;
150+
this.hash = hash;
151+
}
152+
153+
public Entry<K, V> getNext() {
154+
return next;
155+
}
156+
157+
public K getKey() {
158+
return key;
159+
}
160+
161+
public V getValue() {
162+
return value;
163+
}
164+
165+
public int getHash() {
166+
return hash;
167+
}
168+
169+
}
170+
171+
}

src/com/deepak/data/structures/Arrays/BasicOperations.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
2-
* Data-Structures-in-Java
2+
* Data-Structures-In-Java
3+
* BasicOperations.java
34
*/
45
package com.deepak.data.structures.Arrays;
56

@@ -146,7 +147,6 @@ public static void main(String[] args) {
146147
int[] z = new int[x.length];
147148
System.arraycopy(x, 0, z, 0, x.length);
148149
System.out.println("Using System.ArrayCopy() method to copy original array " + Arrays.toString(x) + " to new one => " + Arrays.toString(z));
149-
150150
}
151151

152152
}

src/com/deepak/data/structures/Arrays/CustomArrayList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Data-Structures-in-Java
2+
* Data-Structures-In-Java
33
* CustomArrayList.java
44
*/
55
package com.deepak.data.structures.Arrays;

test/com/deepak/data/structures/Arrays/ArrayListTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Data-Structures-in-Java
2+
* Data-Structures-In-Java
33
* ArrayListTest.java
44
*/
55
package com.deepak.data.structures.Arrays;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Data-Structures-In-Java
3+
* AssociativeArrayTest.java
4+
*/
5+
package com.deepak.data.structures.Arrays;
6+
7+
import org.junit.Assert;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
11+
/**
12+
* Test cases for associative array
13+
*
14+
* @author Deepak
15+
*/
16+
public class AssociativeArrayTest {
17+
18+
/* Associative array defined */
19+
AssociativeArray<String, String> associativeArray;
20+
21+
/**
22+
* Method to setup associative array
23+
*/
24+
@Before
25+
public void setup() {
26+
associativeArray = new AssociativeArray<>(3);
27+
associativeArray.put("A", "Apple");
28+
associativeArray.put("B", "Ball");
29+
associativeArray.put("C", "Cat");
30+
associativeArray.put("D", "Dog");
31+
associativeArray.put("E", "Elephant");
32+
associativeArray.put("F", "Fan");
33+
associativeArray.put("G", "Goat");
34+
}
35+
36+
/**
37+
* Method to test associative Array
38+
*/
39+
@Test
40+
public void testAssociativeArray() {
41+
Assert.assertEquals(associativeArray.size(), 7);
42+
Assert.assertFalse(associativeArray.isEmpty());
43+
Assert.assertEquals(associativeArray.get("D"), "Dog");
44+
associativeArray.put("D", "Dice");
45+
Assert.assertEquals(associativeArray.get("D"), "Dice");
46+
}
47+
48+
}

0 commit comments

Comments
 (0)