The class AbstractMap.SimpleEntry is generic and can be useful.
Answer from Eyal Schneider on Stack OverflowThe class AbstractMap.SimpleEntry is generic and can be useful.
Android programmers could use BasicNameValuePair
Update:
BasicNameValuePair is now deprecated (API 22). Use Pair instead.
Example usage:
Pair<Integer, String> simplePair = new Pair<>(42, "Second");
Integer first = simplePair.first; // 42
String second = simplePair.second; // "Second"
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 ...)
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:

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"
I came across this behavior by accident while I was learning about Map:
public static void mapDemo() {
Map<Long, String> fruitMap = new HashMap<>();
long id = 123;
fruitMap.putIfAbsent(id, "apple");
fruitMap.putIfAbsent(id, "dem apples!");
System.out.println(fruitMap.get(123)); // Prints "null"
System.out.println(fruitMap.get((long) 123)); // Prints "apple"
var num = 123;
} Output:
null apple
When I try to retrieve the value stored against the key "123" - it depends what the type of the key is along with the value.
If I retrieve it by just using a stray number "123" it retrieves the value `null`
BUT if I type cast this same magic number to "long" type then the stored value "apple" is retrieved.
I understand that for when a stray number of this size gets inferred, it is inferred to "int" type BUT why does it matter? (For example. variable `num` gets inferred to "int" type here)
Please can someone give me an explanation for why does this happen?
Why does the type of key also matter when fetching a value in Map?