Reputation: 4220
Howdy, I am trying to understand this method signature:
public <K> Map<K, String> getMulti(Serializer<K> keySerializer, K... keys)
For the following code block:
public <K> Map<K, String> getMulti(Serializer<K> keySerializer, K... keys) {
MultigetSliceQuery<K, String, String> q = createMultigetSliceQuery(keyspace, keySerializer, serializer, serializer);
q.setColumnFamily(CF_NAME);
q.setKeys(keys);
q.setColumnNames(COLUMN_NAME);
QueryResult<Rows<K, String, String>> r = q.execute();
Rows<K, String, String> rows = r.get();
Map<K, String> ret = new HashMap<K, String>(keys.length);
for (K k : keys) {
HColumn<String, String> c = rows.getByKey(k).getColumnSlice().getColumnByName(COLUMN_NAME);
if (c != null && c.getValue() != null) {
ret.put(k, c.getValue());
}
}
return ret;
}
I am not sure what <K>
in the method declaration represents, or what K...
is suppose to mean. Can anyone shed some light on these?
Upvotes: 2
Views: 2397
Reputation: 1108782
The K
is the type parameter of this method. It can be anything you want. E.g. Integer
. In that case you need to visually (in your mind) substitute all K
's in the method signature with Integer
to understand what it's taking and returning. With Integer
as K
, the method will then behave as follows:
public Map<Integer, String> getMulti(Serializer<Integer> keySerializer, Integer... keys)
The ...
in turn is the varargs syntax. It allows you to pass zero-more arguments of or an array of the given type into the method. E.g.
getMulti(serializer);
getMulti(serializer, k1);
getMulti(serializer, k1, k2, k3);
Upvotes: 4
Reputation: 45576
K
here stands for 'key'.
Generic map declaration usually have a signature Map<K,V>
, where K
stands for arbitrary key type and V
for arbitrary value type.
In your case value type is bounded to String
.
Upvotes: 0
Reputation: 66886
K
is a generic type. <K>
just declares that the declaration will talk about some generic type called K
. Here the coder can declare restrictions on K
like <K extends List>
, or not.
Then K
is simply used as a placeholder, a generic type. When you call this with a Serializer<String>
for example, then imagine that the method will be invoked as if K
were replaced with the type String
throughout the method.
That's all that's going on as regards K
.
If you want an interpretation of the method signature in English: it takes a Serializer
that operates on K
s, and zero or more K
s. It returns a mapping from K
s to String
s.
Upvotes: 3
Reputation: 6124
K simply denotes the presence of generics -- this method operates on a generic class K. It takes a serializer operating on the generic class K and a variable array of arguments of type K and it returns a map which maps instances of K to Strings.
Upvotes: 0