Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Equivalent to Python Dictionaries

I am a long time user of Python and really like the way that the dictionaries are used. They are very intuitive and easy to use. Is there a good Java equivalent to python's dictionaries? I have heard of people using hashmaps and hashtables. Could someone explain the similarities and differences of using hashtables and hashmaps versus python's dictionaries?

like image 611
slimbo Avatar asked Oct 08 '09 21:10

slimbo


People also ask

What is the Java equivalent of a Python dictionary?

Python's dict class is an implementation of what the Python documentation informally calls "mapping types". Internally, dict is implemented using a hashtable. Java's HashMap class is an implementation of the Map interface. Internally, HashMap is implemented using a hashtable.

Are there dictionaries in Java?

In Java, Dictionary is the list of key-value pairs. We can store, retrieve, remove, get, and put values in the dictionary by using the Java Dictionary class.

Is HashMap same as Python dictionary?

Yes, it is a hash mapping or hash table. You can read a description of python's dict implementation, as written by Tim Peters, here.

Is dictionary same as HashMap?

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.


1 Answers

Python's dict class is an implementation of what the Python documentation informally calls "mapping types". Internally, dict is implemented using a hashtable.

Java's HashMap class is an implementation of the Map interface. Internally, HashMap is implemented using a hashtable.

There are a few minor differences in syntax, and I believe the implementations are tuned slightly differently, but overall they are completely interchangeable.

like image 50
Daniel Pryden Avatar answered Oct 12 '22 15:10

Daniel Pryden