I want to declare a generic class that will work on triplets - key
, value
and metadata
.
The key
and value
fields are mandatory but the metadata
field is optional.
class Triplet<K,V,M>{
K key;
V value;
M metadata;
//setters and getters
}
While using the above class I have to initialize it like below -
Triplet<Integer, String, String> t1 = new Triplet<>();
// Setters
But for some use cases metadata
is optional. So when I use null
as the 3rd type argument, the compiler gives an error -
Triplet<Integer, String, null> t2 = new Triplet<>();
How should I correctly instantiate a parameterized type that works for multiple types, where one of the type arguments specified at the use-site is optional?