119

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?

user
  • 1,220
  • 1
  • 12
  • 31
slimbo
  • 2,699
  • 4
  • 25
  • 36

4 Answers4

140

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.

Daniel Pryden
  • 59,486
  • 16
  • 97
  • 135
  • 67
    No example provided :( – Kamran Bigdely Sep 13 '16 at 22:07
  • 2
    @kami: What kind of example would you want? – Daniel Pryden Sep 14 '16 at 03:40
  • 12
    Any example that illustrates a java equivalent of python's dictionary in action. A useful answer include some example because most people come here to see examples and use them in their code. – Kamran Bigdely Sep 14 '16 at 16:26
  • 21
    @kami: I don't think that's right. The question doesn't ask "what is the equivalent Java code for some specific Python code". In fact the question doesn't contain any code at all. I'm not sure what the value would be of adding examples of using the Map API in Java; this answer already links to the canonical documentation. If you want to learn Java, start with a tutorial, not Stack Overflow. I certainly don't think this answer warrants a downvote just because it lacks a copy-pasteable code sample. – Daniel Pryden Sep 14 '16 at 16:26
  • 4
    Sad to read that comment saying "most people come here to see examples and *use them in their code*", I hope this is wrong. – 0xc0de May 21 '19 at 05:21
  • 1
    @0xc0de unfortunately most people really do just copy and paste. – KingLogic Jun 18 '21 at 05:27
  • 2
    Regardless of whether people want to copy-paste, an example usually helps in understanding anything better, in conversations in general, and programming. in particular. The number of upvotes on this answer is an indication of the level of interest among a population much larger than just the OP. But I do agree it doesn't warrant a downvote – fountainhead Aug 12 '22 at 21:02
45

The idea of dictionary and Map is similar. Both contain elements like

key1:value1, key2:value2 ... and so on

In Java, Map is implemented different ways like HashMap, or TreeMap etc. put(), get() operations are similar

import java.util.HashMap;

Map map = new HashMap();
// Put elements to the map
map.put("Ram", new Double(3434.34));
map.put("Krishna", new Double(123.22));
map.put("Hary", new Double(1378.00));
//to get elements
map.get("Krishna"); // =123.22
map.get("Hary"); // = 1378.00 

See documentation of HashMap in java8 https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
Hary Bakta
  • 499
  • 4
  • 5
6

One difference between the two is that dict has stricter requirements as to what data types can act as a key. Java will allow any object to work as a key -- although you should take care to ensure that the object's hashCode() method returns a unique value that reflects its internal state. Python requires keys to fit its definition of hashable, which specifies that the object's hash code should never change over its lifetime.

Tim Clemons
  • 6,231
  • 4
  • 25
  • 23
  • 1
    This is true, but it's not actually enforced by either language. Obviously in either a Java `hashCode()` method or in a Python `__hash__()` method, you should try to return a unique value that reflects internal state. In either Java or Python, if you have a mutable object, it probably shouldn't be a hashtable key, so it makes sense to throw an exception from the `hashCode()` or `__hash__()` methods. – Daniel Pryden Oct 09 '09 at 21:57
  • 1
    In my experience, almost anything in Python can be a dict key ... what's the 'stricter requirement'? – Ron Kalian Mar 19 '19 at 11:38
5

As far as I'm aware (I don't actually use java) dictionaries are just another name for a hashmap/hashtable.

Grabbing code from http://www.fluffycat.com/Java/HashMaps/ it seems they are used in a very similar manner, with a bit of extra java boiler-plate.

berezovskyi
  • 3,133
  • 2
  • 25
  • 30
Tabitha
  • 2,554
  • 1
  • 21
  • 21
  • 3
    Java even has a Dictionary interface which is implemented by Hashtable. HashMap is generally preferred, though. – Michael Myers Oct 08 '09 at 21:48
  • 1
    @Michael Myers : Dictionary is deprecated, Oracle recommends to use Map instead http://docs.oracle.com/javase/7/docs/api/java/util/Dictionary.html – Broken_Window Mar 26 '15 at 16:02