public class Pair extends Object implements Comparable
An example use of this class could be to represent classification of hardware systems as an OS/Version pair of String objects.
Note: If instances of this class will be used in collection classes, you must make sure that the encapsulated primary and secondary objects implement the Comparable interface. In addition, it is recommended to have these classes also override equals() and hashCode() methods defined in class Object.
compareTo(Object)
Constructor and Description |
---|
Pair()
Constructs a Pair with primary and secondary objects set to null.
|
Pair(Object primary,
Object secondary)
Constructs a Pair with the given primary and secondary values.
|
Modifier and Type | Method and Description |
---|---|
int |
compareTo(Object another)
Compares this object with the specified object for order.
|
boolean |
equals(Object object)
Compares this Pair to the specified object.
|
Object |
getPrimary()
Returns the primary object
|
String |
getPrimaryAsString()
If the primary object is a String returns the String.
|
Object |
getSecondary()
Returns the secondary object
|
String |
getSecondaryAsString()
If the secondary object is a String returns the String.
|
int |
hashCode()
Returns a hash code value for the object.
|
void |
setPrimary(Object primary)
Sets the primary object to the given value
|
void |
setSecondary(Object secondary)
Sets the secondary object to the given value
|
String |
toString()
Returns a string form of the primary and secondary objects.
|
public Pair()
public Object getPrimary()
public String getPrimaryAsString()
Primary
- object as String or null if it's not a string.public void setPrimary(Object primary)
primary
- Primary objectpublic Object getSecondary()
public String getSecondaryAsString()
Secondary
- object as String or null if it's not a string.public void setSecondary(Object secondary)
secondary
- Secondary objectpublic boolean equals(Object object)
public int compareTo(Object another)
First invokes the compareTo() method on the primary member of this object to compare it with the primary member of the passed object. If the result is non-zero, it returns with this result. If the result is zero, it then invokes the same method on the secondary member of this object to compare that with the secondary member of the passed object. Returns the result of this second comparison.
In essence, the ordering is based on the primary object. The primary object being the same, the ordering is then based on the secondary object. A null primary/secondary object is assumed to be less than a non-null primary/secondary object.
Note: If you will be using instances of this class in collection classes, then you must make sure that both primary and secondary objects are instances of classes that implement the Comparable interface. You must also make sure that all Pair instances in the collection have primary objects of the same type and secondary objects of the same type.
compareTo
in interface Comparable
another
- Object to compare this instance to.NullPointerException
- If the passed object is nullClassCastException
- If the specified object's type prevents it from being
compared to this instance or if at least one of the primary
and secondary objects of the two compared Pair instances
does not implement the Comparable interface.Comparable.compareTo(Object)
public int hashCode()
Copyright © 2003, 2023, Oracle and/or its affiliates.