# Ordered Pair in Java

In this article, we are going to learn the implementation of ordered pairs in the Java programming language. By the end of the article, there will be a clear insight into the ordered pairs at which particular places and situations are used in Java.

## What is an Ordered Pair?

The word " ordered pair " in the mathematical sense actually refers to a composition of values of X and Y coordinates written in a particular order within parentheses, i.e., "()".The Ordered pairs help the user in locating a certain point on the cartesian plane, i.e., the X Y coordinate generally. The values pointed on the plane can either be integers or fractions. An ordered pair is represented as " p = (x, y)", where " p " is a point such that it lies on the cartesian plane.

## How can we use Ordered Pairs in Java?

A programmer couldn't use the ordered pairs in Java with ease until the release of Java 7. In the previous versions of Java, it was very hard in order to implement the concept of the Ordered pairs within a program in Java. With the entry or introduction of Java 7 into the Java programming ecosystem, " pair class " in Java came into existence. The " javafx.util.Pair " package contains the pair class, using which we can store the values into a pair. To store the values in a pair, we make use of the parameterized constructor, which is generously provided in the package, as mentioned earlier. The constructor takes two arguments, namely, a key and a value corresponding to it. So, the number of parameters that are basically passed through the class " pair " is two. Let us understand the concept of Ordered pairs and pair class in more detail by learning the methods that are provided within the " javafx.util.Pair " package.

## Methods provided within javafx.util.Pair :

1. Pair ( K key, V value ) : The method is used to create a new pair in Java programming.
2. Boolean equals() : By looking at the method, we can confidently say that the method is used to compare two pairs. It equals the values of the second pair to the first pair and returns output either " true " or " false ". The " boolean equals() " method performs a deep comparison of the given pairs on the basis of the values stored in the ordered pair objects.
3. String toString() : It is a method that returns the String representation of the Ordered pair.
4. K getKey(): It is a method to return the key stored in the ordered pair object.
5. V getValue(): It is a method to return the value stored in the ordered pair object.
6. Int hashcode() : This method of the javafx.util.Pair package generates a hash code for a given pair.

### Example program for Boolean equals():

Output:

```false
```

There are different types of classes dealing with the concept of Ordered pairs, and they are also provided within the package " javafx.util ".Let us discuss a few of those classes and their declaration syntaxes.

### Pair class:

The class " Pair " can be found within the package " javafx.util ", and the constructor inscribed within the class is eligible to consider two arguments as parameters, out of which one is an Integer value and the other is a String value. The first one will intend to be a key, and the second one will be the corresponding value of the key.

Syntax:

Example program 1 that demonstrates Pair class:

Output:

```The Integral value is 5
The String value is Hello
```

Example program 2 that demonstrates Pair class:

Output:

```50 Program
```

### Types of Pair class in java:

Depending on the modification of the values involved with the Pair class, the Pair class is classified into two types. They are

1. Mutable Pair
2. Immutable Pair

Mutable Pair:

The Pair class in which the declared set of Pairs' data can be modified without changing the lines of code is called as Mutable Pair class. There are certain methods that help in provoking the above-discussed statement. They are getter and setter methods. The getter methods print the existing data of the Pair object, whereas the setter methods set new values to the same object and replace the existing values or data. The method " getValue() " is one of the getter methods which helps in printing the values. The method " setValue() " is one of the setter methods which helps in setting the new values lying with the same object.

Immutable Pair:

The Pair class in which the declared set of Pairs' data cannot be modified without changing the lines of code is called as Mutable Pair class. They are only getter methods but not the setter methods in the Immutable Pair class. The getter methods print the existing data of the Pair object, whereas the setter methods set new values to the same object and replace the existing values or data. As the Immutable Pair class does not allow setter methods, the method " setValue() " cannot be accessed by the object created within the Pair class. The method " getValue() " is one of the getter methods which helps in printing the values.

Example program:

Output:

```12 16
```

All the programs discussed above are related to the functions and classifications of the class " Pair ". It must be used in a way that the compiler or an IDE supports it completely. If not, the program always shows an error defining that the Pair class is irregular.

### Feedback   