Map<K, V>
is a parameterised type, which means that it represents a whole family of different types, such as
Map<String, Integer>
- a Map
where the keys are String
s and the values are Integer
s
Map<Integer, LocalDate>
- a Map
where the keys are Integer
s and the values are LocalDate
s
and so on. But there's a kind of sameness about all these types - they're all a Map
where the keys have some type and the values have some other type.
Now the processing inside the Map
is going to be much the same, regardless of the type of its keys or the type of its values. So none of the code inside Map.class
should specify what those types are. Instead, it uses K
to represent whatever type the keys will be, and V
to represent whatever type the values will be. These letters are called type parameters, and they are used in Map.class
as a way to refer to the types that will be used in the Map
.
For example, if you have a Map<String, Integer>
, then K
means String
and V
means Integer
. If you have a Map
with different key type and value type, then K
and V
will mean something else.
In the method that you've shown in your question, k
is a local variable of the type represented by K
and v
is a local variable of the type represented by V
. Again, if you have a Map<String, Integer>
, k
is effectively a String
variable, and v
is an Integer
variable. But these could actually be any types at all.
Where you've written //more code follows...
the code in question will just be pulling a key and value out of the Map
, pointing the variables k
and v
at them, and then applying the BiConsumer
called action
to them.