什么是键值对? [英] What is a Key-Value Pair?

查看:1126
本文介绍了什么是键值对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在自学Java,因此我可以对android应用程序进行编程,并遇到了使用键值对的需求,但不知道它是什么.我尝试查找它,但是找不到任何好的资源来解释它.

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.

任何人都可以向我解释一下,或者将我指向可以阅读的资源吗? 谢谢!

Would anyone be able to explain this to me, or point me to a resource where I can read up on it? Thanks!

我很困惑的是这与普通变量有何不同.说一个String类型的变量指向一个对象,或者int变量指向int值.

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?

推荐答案

在最简单的级别上,键值对只是两个值,其中一个已指定为键",另一个已指定为键"成为价值".

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.

后续问题:

这与数组相同吗?

Is this the same as an array?

好吧...数组可以看作是从一组索引(整数)到值的映射.但是映射更为笼统.而且在Java中,数组还有其他属性可以将它们与Map区别开来……并且它们具有更加简单,快速且耗费内存的实现.

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 Maps ... and they have a much simpler, faster, and less memory-hungry implementation.

(请注意,在某些语言中,本身没有数组"数据类型.相反,原语是哈希"或关联数组"……这是更通用的映射.)

(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?

不.键可以是任何类型. (使用可变类型作为键通常是一个[em>坏主意,特别是如果您的映射是使用Java中的标准Map类型之一实现的.但是,即使在某些情况下也可以使用)

No. A key can be any type. (It is generally a bad idea to 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.)

说一个String类型的变量指向一个对象,或者int变量指向int值.那个变量不是键",对象不是值"吗?

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"?

不.或者至少不是使用Java之类的静态语言.将键值对与变量绑定区分开的是键"对象是数据,因此可以采用不同的值.相比之下,变量的名称在程序的源代码中是硬连接的:您不能在运行时更改它.

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 ...)

这篇关于什么是键值对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆