Java 相当于 Python 字典

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1540673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:58:44  来源:igfitidea点击:

Java Equivalent to Python Dictionaries

javapythonhashdictionary

提问by slimbo

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?

我是 Python 的长期用户,非常喜欢字典的使用方式。它们非常直观且易于使用。是否有一个很好的 Java 相当于 python 的字典?我听说有人使用哈希图和哈希表。有人可以解释使用哈希表和哈希图与 python 字典的异同吗?

采纳答案by Daniel Pryden

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

Python 的dict类是 Python 文档非正式地称为“映射类型”的实现。在内部,dict使用哈希表实现。

Java's HashMapclass is an implementation of the Mapinterface. Internally, HashMapis implemented using a hashtable.

Java 的HashMap类是Map接口的实现。在内部,HashMap使用哈希表实现。

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

语法上有一些细微的差异,我相信实现的调整略有不同,但总的来说它们是完全可以互换的。

回答by Tabitha

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

据我所知(我实际上并没有使用 java)字典只是 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.

http://www.fluffycat.com/Java/HashMaps/抓取代码似乎它们以非常相似的方式使用,还有一些额外的 java 样板。

回答by Tim Clemons

One difference between the two is that dicthas 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.

两者之间的一个区别是dict对哪些数据类型可以充当键有更严格的要求。Java 将允许任何对象用作键——尽管您应该注意确保对象的hashCode()方法返回反映其内部状态的唯一值。Python 需要键来适应它对hashable 的定义,它指定对象的哈希码在其生命周期内永远不应该改变。

回答by Hary Bakta

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

字典和地图的思想是相似的。两者都包含类似的元素

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

In Java, Mapis implemented different ways like HashMap, or TreeMapetc. put(), get()operations are similar

在 Java 中,Map是通过不同的方式实现的,比如HashMap, 或者TreeMapput(), get()操作是相似的

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

请参阅 java8 https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html 中的 HashMap 文档