You seem to need some Maps rather than Lists. Check the Javadoc for Map implementations; the most common is HashMap, but there are sorted, concurrent, deterministically iterable implementations etc. available too.
ListObject: every key corresponds to a String value; key is a string itself
Map<String, String>
ListObject: every key corresponds to an array of Strings; key is a string itself
Map<String, String[]>
(or preferably Map<String, List<String>>)
ListObject: same deal but with two value fields per key.
Map<String, UserDefinedClassWithTwoFields>
Answer from Péter Török on Stack OverflowYou seem to need some Maps rather than Lists. Check the Javadoc for Map implementations; the most common is HashMap, but there are sorted, concurrent, deterministically iterable implementations etc. available too.
ListObject: every key corresponds to a String value; key is a string itself
Map<String, String>
ListObject: every key corresponds to an array of Strings; key is a string itself
Map<String, String[]>
(or preferably Map<String, List<String>>)
ListObject: same deal but with two value fields per key.
Map<String, UserDefinedClassWithTwoFields>
Map<KeyType,ValueType> which is implemented by HashMap<KeyType, ValueType> and TreeMap<KeyType, ValueType>, among others -- HashMap is unordered and TreeMap is ordered.
Other useful Maps are LinkedHashMap which is like HashMap but iterates in insertion order, and com.google.common.collect.Maps in Guava which has a bunch of utility methods, and com.google.common.collect.ImmutableMap which is an immutable map implementation.
For your key corresponding to an array of strings, you might want to look at a Multimap which is a map with multiple values for a given key.
An enum would suit best:
public enum Response {
OK(200, "OK"),
Created(201, "Created"),
NotFound(404, "Not found");
private final int _code;
private final String _message;
Response(int code, String message) {
_code = code;
_message = message;
}
public int code() {
return _code;
}
public String message() {
return _message;
}
}
Additional benefit of an enum is that you would operate with comprehensible constant names in code, such as Response.NotFound rather than numerical codes. If you need to really get a value by code, just add a static method to resolve the enum instance.
You can use static class initializer and use a HashMap, like u tried.
class Response
{
public static final Map<Integer,String> STATUS;
static{
STATUS=new HashMap<>();
STATUS.put(200,"OK");
STATUS.put(201,"Created");
// ...
}
}
Example usage:
Response.STATUS.get(200); // will return "OK"
If you want, you can even make it unmodifiable with Collection.unmodifiableMap(). That is how I would do it. So the static class initializer from above would now look like this:
static{
HashMap<Integer,String> statusValues=new HashMap<>();
statusValues.put(200,"OK");
statusValues.put(201,"Created");
// ...
STATUS=Collections.unmodifiableMap(statusValues);
}
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"