java 什么是键值对?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25955749/
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
What is a Key-Value Pair?
提问by RedMist
I am teaching myself Java so I can program android applications and came across the need to use a key value pair, but don't know what it is. I tried looking it up but cannot find any good resources to explain it.
我正在自学 Java,因此我可以编写 android 应用程序并遇到需要使用键值对,但不知道它是什么。我尝试查找它,但找不到任何好的资源来解释它。
Would anyone be able to explain this to me, or point me to a resource where I can read up on it? Thanks!
任何人都可以向我解释这一点,或者指出我可以阅读的资源吗?谢谢!
edit: what I am confused about is how this is different from a normal variable. Say a variable of type String points to an object or int variable points to int value. Isn't that variable a "key", and the object a "value?
编辑:我感到困惑的是这与正常变量有何不同。假设 String 类型的变量指向一个对象或 int 变量指向 int 值。那个变量不是“键”,对象不是“值”吗?
回答by nem035
A key-value pair is two values usually connected in such a way that the value is accessed using the key. They are commonly used in various data-structures to provide fast access to values. Check out hashtable data structure
for instance (in Java, you can take a look at HashMap
or Hashtable
)
键值对是两个值,通常以使用键访问值的方式连接。它们通常用于各种数据结构中,以提供对值的快速访问。查看hashtable data structure
例如(在 Java 中,您可以查看HashMap
或Hashtable
)
For example, let's say we have a structure CartoonCharacter
holding the mentioned pairs like:
例如,假设我们有一个CartoonCharacter
包含上述对的结构,例如:
This key-value relationship would look something like:
这种键值关系类似于:
CartoonCharacter[lastName] = "Bunny";
So if you want to get the value Bunny
you would access it through the key lastName
.
因此,如果您想获取该值,Bunny
您可以通过 key 访问它lastName
。
key = "lastName"
value = CartoonCharacter.get(key)
print (value) // this would print "Bunny"
回答by Stephen C
At the simplest level, a key-value pair is just two values, one of which you have designated to be a "key" and the other you have designated to be the "value".
在最简单的层面上,键值对只是两个值,其中一个被指定为“键”,另一个被指定为“值”。
However, it is more common to talk about key-value pairs in the context of a mapping, i.e. a (mathematical) function which maps from a key to the corresponding value or values. Depending on the properties of this mapping, you may constrain the set of key-value pairs. For example, for a 1-to-1 mapping, you need the keys in the set to be unique.
然而,在映射的上下文中谈论键值对更为常见,即从一个键映射到一个或多个相应值的(数学)函数。根据此映射的属性,您可以限制键值对集。例如,对于 1 对 1 映射,您需要集合中的键是唯一的。
Follow-up questions:
后续问题:
Is this the same as an array?
这和数组一样吗?
Well ... an array could be considered as a mapping from a set of indexes (integers) to values. But a mapping is more general. And in Java, arrays have other properties that distinguish them from Map
s ... and they have a much simpler, faster, and less memory-hungry implementation.
嗯……一个数组可以被认为是从一组索引(整数)到值的映射。但是映射更通用。而在 Java 中,数组还有其他属性可以将它们与Map
s区分开来……而且它们有一个更简单、更快、内存占用更少的实现。
(Note that in some languages, there is no "array" data type per-se. Instead, the primitive is a "hash" or an "associative array" ... which is a more general map.)
(请注意,在某些语言中,本身没有“数组”数据类型。相反,原语是“散列”或“关联数组”……这是一种更通用的映射。)
And is a key always a string?
键总是字符串吗?
No. A key can be any type. (It is generally a bad ideato use a mutable type as a key, especially if your mapping is implemented using one of the standard Map
types in Java. However, even that can work in some circumstances.)
不可以。密钥可以是任何类型。(使用可变类型作为键通常是一个坏主意,特别是如果您的映射是使用Map
Java中的标准类型之一实现的。但是,即使在某些情况下也可以工作。)
Say a variable of type String points to an object or int variable points to int value. Isn't that variable a "key", and the object a "value"?
假设 String 类型的变量指向一个对象或 int 变量指向 int 值。那个变量不是“键”,对象不是“值”吗?
No. Or at least, not in a static language like Java. The thing that distinguishes a key-value pair from a variable binding is that the "key" object is data, and hence can take different values. By contrast, a variable's name is hard-wired in the source code of your program: you can't change it at runtime.
不。或者至少,不是像 Java 这样的静态语言。键值对与变量绑定的区别在于“键”对象是数据,因此可以采用不同的值。相比之下,变量的名称在程序的源代码中是固定的:您不能在运行时更改它。
(In some dynamic languages, you can create new variables dynamically (at runtime), and for such languages you could argue that variables are key-value pairs in a mapping that represents the scope ... at some point in the program's execution. But Java isn't like that ...)
(在某些动态语言中,您可以(在运行时)动态创建新变量,对于此类语言,您可能会争辩说变量是表示范围的映射中的键值对……在程序执行的某个时刻。但是Java不是那样的......)