What is a Key-Value Pair?

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.

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?

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.


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

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

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

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.

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

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Extract value from NSObject key/value pair

분류에서Dev

How to add a key,value pair to a list?

분류에서Dev

Convert array to Hashmap (key value pair) javascript

분류에서Dev

how to create variables from key value object pair in javascript

분류에서Dev

How to handle a file using key,value pair in shell script

분류에서Dev

jQuery AJAX - push additional key/value pair into a serialized $_POST array

분류에서Dev

Filter a list of key-value pair in a selected node in Ne04j

분류에서Dev

How to remove a simple specific key value pair from all objects inside an array

분류에서Dev

AngularJS nested ng-repat in ng-repeat (key, value) pair data structure in HTML table element with sort filter

분류에서Dev

In Swift, what is the best way to assign two arrays to key/value of a dictionary?

분류에서Dev

What prevents me from adding a row with foreign key value?

분류에서Dev

What is the ^@ key

분류에서Dev

what is the fastest way to compare a pair struct

분류에서Dev

What data structure to use to have O(log n) key AND value lookup?

분류에서Dev

using array_key_exists to pair up the amount total to id

분류에서Dev

kwargs of key : set pair의 사전 목록

분류에서Dev

What is the use of a hash key with ":"

분류에서Dev

Sum by Key Value in r

분류에서Dev

MDX Attribute Value Key

분류에서Dev

Use the value of a key as a key in mongodb aggregation

분류에서Dev

What is a OpenPGP/GnuPG key ID?

분류에서Dev

Storing Key-Value and Value-Key dictionaries in a single table

분류에서Dev

Storing Key of TreeMap in Value (java)

분류에서Dev

From string to Dictionary key and value

분류에서Dev

Swift Key Value - advice needed

분류에서Dev

Function not returning the Value of its Key

분류에서Dev

what this integer value means?

분류에서Dev

EC Key Pair Generation Java 코드 및 JavaKeyStore Explorer 애플리케이션

분류에서Dev

ServiceResource create_key_pair에서 개인 키 가져 오기

Related 관련 기사

  1. 1

    Extract value from NSObject key/value pair

  2. 2

    How to add a key,value pair to a list?

  3. 3

    Convert array to Hashmap (key value pair) javascript

  4. 4

    how to create variables from key value object pair in javascript

  5. 5

    How to handle a file using key,value pair in shell script

  6. 6

    jQuery AJAX - push additional key/value pair into a serialized $_POST array

  7. 7

    Filter a list of key-value pair in a selected node in Ne04j

  8. 8

    How to remove a simple specific key value pair from all objects inside an array

  9. 9

    AngularJS nested ng-repat in ng-repeat (key, value) pair data structure in HTML table element with sort filter

  10. 10

    In Swift, what is the best way to assign two arrays to key/value of a dictionary?

  11. 11

    What prevents me from adding a row with foreign key value?

  12. 12

    What is the ^@ key

  13. 13

    what is the fastest way to compare a pair struct

  14. 14

    What data structure to use to have O(log n) key AND value lookup?

  15. 15

    using array_key_exists to pair up the amount total to id

  16. 16

    kwargs of key : set pair의 사전 목록

  17. 17

    What is the use of a hash key with ":"

  18. 18

    Sum by Key Value in r

  19. 19

    MDX Attribute Value Key

  20. 20

    Use the value of a key as a key in mongodb aggregation

  21. 21

    What is a OpenPGP/GnuPG key ID?

  22. 22

    Storing Key-Value and Value-Key dictionaries in a single table

  23. 23

    Storing Key of TreeMap in Value (java)

  24. 24

    From string to Dictionary key and value

  25. 25

    Swift Key Value - advice needed

  26. 26

    Function not returning the Value of its Key

  27. 27

    what this integer value means?

  28. 28

    EC Key Pair Generation Java 코드 및 JavaKeyStore Explorer 애플리케이션

  29. 29

    ServiceResource create_key_pair에서 개인 키 가져 오기

뜨겁다태그

보관