2

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.

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
  • 698,415
  • 94
  • 811
  • 1,216
RedMist
  • 73
  • 1
  • 1
  • 4
  • What does your tutorial say? – AntonH Sep 21 '14 at 04:20
  • 6
    What does an internet search say? Literally millions of hits!! – Mitch Wheat Sep 21 '14 at 04:21
  • A key-value pair is two linked data items: a key, which is a unique identifier for some item of data, and the value, which is either the data that is identified or a pointer to the location of that data.one hit that says it all – AlphaWolf Sep 21 '14 at 04:22
  • It is a pair consisting of a key and a value. Nothing mysterious. – user207421 Sep 21 '14 at 04:38
  • 2
    Read [the Java Tutorials: Collections](http://docs.oracle.com/javase/tutorial/collections), specifically, read about [Maps](http://docs.oracle.com/javase/tutorial/collections/interfaces/map.html) – Barranka Sep 21 '14 at 04:40

2 Answers2

9

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

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
5

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)

For example, let's say we have a structure CartoonCharacter holding the mentioned pairs like:

bugs_bunny

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.

key = "lastName"
value = CartoonCharacter.get(key)
print (value) // this would print "Bunny"
nem035
  • 34,790
  • 6
  • 87
  • 99
  • This makes it a bit more clear to me now. But, is this the same as an array? – RedMist Sep 21 '14 at 04:40
  • Array is an index to value. Map is key to value – shinjw Sep 21 '14 at 04:41
  • So the difference is that in a key value pair I can define what points to each value whereas in a array I can't (it must be some index number)? And is a key always a string? – RedMist Sep 21 '14 at 04:45
  • Well, you can define which key points to which value in a Map but you can do that in an array as well (using the index). However, in a Map pretty much any object can be a key while in arrays, only integer can be the index – nem035 Sep 21 '14 at 04:48