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 pairs of things. The class should provide a constructor, a method for getting the first member of the pair, a method for getting the second member of the pair, a method for setting the first member of the pair, a method for setting the second member of the pair. The class should be parameterised over two types one for the first member and one for the second member of the pair.
Is this a correct implementation for this question ?
public class Pair<firstThing, secondThing>{
private firstThing first;//first member of pair
private secondThing second;//second member of pair
public Pair(firstThing first, secondThing second){
this.first = first;
this.second = second;
}
public void setFirst(firstThing first){
this.first = first;
}
public void setSecond(secondThing second) {
this.second = second;
}
public thing getFirst() {
return this.first;
}
public thing getSecond() {
return this.second;
}
}
Usually a generic Pair type has two generic type parameters, not one - so you could have (say) a Pair<String, Integer> . That's typically more useful, IMO. I would also suggest that you think about a more conventional name for your type parameter than "thing". For example, you might use Pair<A, B> or Pair<T, U> .
A Generic class simply means that the items or functions in that class can be generalized with the parameter(example T) to specify that we can add any type as a parameter in place of T like Integer, Character, String, Double or any other user-defined type.
Generic classes encapsulate operations that are not specific to a particular data type. The most common use for generic classes is with collections like linked lists, hash tables, stacks, queues, trees, and so on.
Pair class represents a Tuple with two elements.
Almost. I'd write it like this:
public class Pair<F, S> {
private F first; //first member of pair
private S second; //second member of pair
public Pair(F first, S second) {
this.first = first;
this.second = second;
}
public void setFirst(F first) {
this.first = first;
}
public void setSecond(S second) {
this.second = second;
}
public F getFirst() {
return first;
}
public S getSecond() {
return second;
}
}
Edit: I agree with @karmakaze's comment. The code should skip the setters and make first and second final to keep it immutable.
The need for a Pair class usually crops up in larger projects - I'm about to (re)implement one for the current project (as previous implementations are not accessible).
Generally I make it an immutable POJO, with a convenience function to create instances. For example:
public class Pair<T,U>
{
public final T first;
public final U second;
public static <T,U> Pair<T,U> of(T first, U second);
}
So that the end-user can write:
return Pair.of (a, b);
and
Pair<A,B> p = someThing ();
doSomething (p.first);
doSomethingElse (p.second);
As mentioned above, the Pair class should also implement hashCode(), equals(), optional-but-useful toString(), as possibly clone() and compareTo() for use where these are supported by T and U - though extra work is required to describe how these contracts are supported by the Pair class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With