Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between 2d array and hashmap

Tags:

java

I am relatively new to Java and I just want to make sure I get the basic concepts correctly. So my question is how is hashmap different to 2d array. I will illustrate an example and if someone could possibly correct me if I am wrong that would be great. So

  1. You cannot access/change the 1st array of the 2d array directly in contrast to hashmap. So for example if you have got arr[2][5] the first arr[2] you cannot change it to something else.In other words if we have int arr[2][2] you cannot change it to say arr[Cars][2] whereas with hashmap you can. You cannot even access this at all whereas with hashmap you can. If you have got map Martin, 25 you can possibly make this to Joe, 22 easily.

  2. You can search quite easily in hashmap on the first value. Say if you want to find the age of martin from the previous example you can easily search on Martin and the age 25 will appear.

  3. I have been taught that 2d arrays represent a table. Something like.

arr[2][3]

1 [1 , 2 , 3]

2 [1 , 2 , 3]

But in reality you cannot access/change 1 and 2 outside the [] grid. This should serve only as an imaginary help to illustrate the concept of 2d arrays.

Could you please correct me if I am wrong or make any additional comments on that.

Thank you

like image 733
user3149650 Avatar asked Dec 11 '22 08:12

user3149650


2 Answers

A hashmap uses keys and values, not indices. Therefore you can only search for keys, and thus not access any index. Keys need to be unique, you can not have two identical keys, the old key's value will be replaced if you try to reassign something to it. In a hashmap, key can be any object (an index of an array has to be a number). The key kind of works as the index of an array. As said before, the key can be any object, an array's indexes must be int primitives.

like image 126
Marckvdv Avatar answered Dec 13 '22 22:12

Marckvdv


It's like comparing apples and oranges.

A 2D array is just a bidimensional grid of objects, an HashMap is a special kind of associative array (called also dictionary or map) which associates generic keys to generic values. The HashMap is not the only one existing, a TreeMap, for example, exists too, which provides roughly the same interface but a totally different implementation.

The other main difference is that an HashMap is made to fulfill a specific requirement which is unnecessary in an array: being able to store sparse keys without wasting too much space while keeping complexity of get and set operation constant.

This can be seen easily:

int[] intMap = new int[10];
HashMap<Integer,Integer> hashIntMap = new HashMap<Integer,Integer>();

Now suppose that you want to insert the pair (500,100):

intMap[500] = 100;
hashIntMap.put(500, 100);

In the first case you will need to have enough room in the array (at least 501 elements) to be able to access cell at index 500. In an HashMap there is no such requirement since elements are stored by using an hash code and bucketed in a lot less cells than the required one.

like image 28
Jack Avatar answered Dec 13 '22 23:12

Jack