Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between map and dict

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.

like image 988
frazman Avatar asked Apr 08 '12 20:04

frazman


People also ask

What is the difference between map and dictionary in Python?

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.

Is dictionary similar to map?

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.

What is a hash map vs dictionary?

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.

Why is dictionary called mapping?

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.


2 Answers

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.

like image 151
Nolen Royalty Avatar answered Sep 27 '22 18:09

Nolen Royalty


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

like image 37
Óscar López Avatar answered Sep 27 '22 16:09

Óscar López