I might be confused between hashmap
in Java, and map
/dict
in Python.
I thought that the hash
(k/v abstraction) of Java is kind of the same as dict
in Python
But then what does the map
datatype do?
Is it the same abstraction as the hashmap abstraction? If so, then how is it different from dictionary?
I went through the docs, but it took me to whole together different paradigm: functional programming.
To answer the question in the title, it is the same. A map seen as a datastructure is the same concept as a dict . dict s also use hashes to map keys to values. That's why java developers call it hashmap.
Dictionary and map contrasted So, using the strict Comp Sci terminology above, a dictionary is only a map if the interface happens to support additional operations not required of every dictionary: the ability to store elements with distinct key and value components.
In Java the HashMap implements the Map interface while the Dictionary does not. That makes the Dictionary obsolete (according to the API docs). That is, they both do a similar function so you are right that they seem very similar...a HashMap is a type of dictionary. You are advised to use the HashMap though.
In mathematical language, a dictionary represents a mapping from keys to values, so you can also say that each key “maps to” a value. As an example, we'll build a dictionary that maps from English to Spanish words, so the keys and the values are all strings.
Map is not a datatype in python. It applies a function to a series of values and returns the result.
>>> def f(x): ... return x**2 ... >>> list(map(f, range(5))) [0, 1, 4, 9, 16]
Often for a simple case like that to be "pythonic" we use list comprehensions.
>>> [x**2 for x in range(5)] [0, 1, 4, 9, 16]
You are right in your comparison of hashmaps and dicts.
In essence a Map
in Java is like a dict
in Python: both data structures create associations between keys and values, with expected O(1) performance for the get()
and contains()
operations.
The Map
data structure in Java should not be confused with the map()
function in Python:
map(function, iterable, ...)
Apply function to every item of iterable and return a list of the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With