Just attempting this question I found in a past exam paper so that I can prepare for an upcoming Java examination.
Provide a generic class Pair for representing pair
I implemented something similar but with static builder and chained setters
public class Pair {
private R left;
private L right;
public static Pair of(K k, V v) {
return new Pair(k, v);
}
public Pair() {}
public Pair(R key, L value) {
this.left(key);
this.right(value);
}
public R left() {
return left;
}
public Pair left(R key) {
this.left = key;
return this;
}
public L right() {
return right;
}
public Pair right(L value) {
this.right = value;
return this;
}
}