Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a key object (use in hashmap)?

Could someone explain what exactly a key object is for use in hashmap? There is a method: "put(Object key, Object value) Associates the specified value with the specified key in this map."

so is the key just any object you want? And by value do they mean another object or like a attribute. Just need some further explanation as I am confused! Thanks a bunch

like image 856
WestJackson Avatar asked Oct 23 '22 17:10

WestJackson


1 Answers

You should read up on what a hashmap is. In general, a hash is a data structure for efficiently storing arbitrary data (the values) in a table.

A general problem in storing information in any structure is that of how to quickly look up the data again, once it's in the structure. A hash solves this issue by the use of keys. The key of a value determines where in the table the value will be stored, by way of some hash function. They key is used in a hash in the same way that an index is used in an array:

array[index] => some_value
hash{key} => some_value

In the case of "put(Object key, Object value)", the 'value' object is the data that you want to store and the 'key' object is what you will use to get the data back out of the hash:

MyObject myKey = new MyObject( ... );
MyOtherObject myValue = new MyOtherObject( ... );
...
myHash.put( myKey, myValue );  // add myValue to the hash
...
MyOtherObject data = myhash.get( myKey );  // get myValue out of the hash
like image 161
Christopher Neylan Avatar answered Oct 27 '22 11:10

Christopher Neylan