Package it.unimi.di.prog2.h23
Class Pair<T,U>
java.lang.Object
it.unimi.di.prog2.h23.Pair<T,U>
- Type Parameters:
T
- the type of the first object.U
- the type of the second object.
A class representing an immutable pair of objects.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
Checks if two pairs are equal.boolean
first()
Returns the first object of the pair.static <T extends Comparable<? super T>>
Comparator<Pair<T, ?>> Returns a comparator that compares pairs by their first element.int
hashCode()
static void
A method to test the class.second()
Returns the second object of the pair.static <T extends Comparable<? super T>>
Comparator<Pair<?, T>> Returns a comparator that compares pairs by their second element.static <X,
Y> Pair <Y, X> Swaps the elements of a pair.toString()
-
Field Details
-
first
The first object of the pair. -
second
The second object of the pair.
-
-
Constructor Details
-
Pair
Creates a new pair.- Parameters:
first
- the first object.second
- the second object.- Throws:
NullPointerException
- iffirst
orsecond
isnull
.
-
-
Method Details
-
first
Returns the first object of the pair.- Returns:
- the first object of the pair.
-
second
Returns the second object of the pair.- Returns:
- the second object of the pair.
-
toString
-
equals
-
hashCode
public int hashCode() -
firstComparator
Returns a comparator that compares pairs by their first element.- Type Parameters:
T
- the type of the first element.- Returns:
- a comparator that compares pairs by their first element.
-
secondComparator
Returns a comparator that compares pairs by their second element.- Type Parameters:
T
- the type of the second element.- Returns:
- a comparator that compares pairs by their second element.
-
swap
Swaps the elements of a pair.- Type Parameters:
X
- the type of the first element.Y
- the type of the second element.- Parameters:
pair
- the pair to swap.- Returns:
- a new pair with the elements of
pair
swapped. - Throws:
NullPointerException
- ifpair
isnull
.
-
areEqual
Checks if two pairs are equal.- Parameters:
p1
- the first pair.p2
- the second pair.- Returns:
true
ifp1
andp2
are equal,false
otherwise.- Throws:
NullPointerException
- ifp1
orp2
isnull
.
-
main
A method to test the class.- Parameters:
args
- unused.
-