Tutorial :How to define your own Map in Java [closed]



Question:

I have to write a program who use collection for implementing a map in Java.


Solution:1

Create a type MapEntry that holds a key and a value.

Take some collection class and extend it with methods put(Object key, Object value), get(Object key), remove(Object key) and containsKey(Object key). Parametrize it to be a collection of MapEntries.

First, containsKey: Loop through your collection, looking for a MapEntry that has a key value corresponding to the given key. Return true if you found one, false otherwise.

Next, put: Loop through your collection, looking for a MapEntry with the same key. If you find one, replace the value. If not, create a new MapEntry with the key and value and add it to your collection.

get: Loop through your collection, looking for a MapEntry with the given key. If you find one, return the value, otherwise null.

remove: Loop through... if you find one, remove that map entry.

Done.


Solution:2

So, You want to write a program that uses Map, you can seek it from Oracle Tutorials. Next time, try to be specific and and clear on your question, so as to get better answers...


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »