Pair Class in Java

In C++, we have std::pair in the utility library which is of immense use if we want to keep a pair of values together. We were looking for an equivalent class for pair in Java but Pair class did not come into existence till Java 7. JavaFX 2.2 has the javafx.util.Pair class which can be used to store a pair. We need to store the values into Pair using the parameterized constructor provided by the javafx.util.Pair class.

Note : Note that the <Key, Value> pair used in HashMap/TreeMap. Here, <Key, Value> simply refers to a pair of values that are stored together.

Methods provided by the javafx.util.Pair class

Pair (K key, V value) : Creates a new pair

boolean equals() : It is used to compare two pair objects. It does a deep comparison, i.e., it compares on the basic of the values (<Key, Value>) which are stored in the pair objects.

Example:

filter_none

editclose

play_arrow

linkbrightness_4code

Pair p1 = newPair(3,4);

Pair p2 = newPair(3,4);

Pair p3 = newPair(4,4);

System.out.println(p1.equals(p2) + “ ” + p2.equals(p3));

chevron_right

filter_none

Output:
true false

String toString() : This method will return the String representation of the Pair.

K getKey() : It returns key for the pair.

V getValue() : It returns value for the pair.

int hashCode() : Generate a hash code for the Pair.

Let us have a look at the following problem.Problem Statement : We are given names of n students with their corresponding scores obtained in a quiz. We need to find the student with maximum score in the class.

Note : You need to have Java 8 installed on your machine in order to run the below program.

filter_none

editclose

play_arrow

linkbrightness_4code

/* Java program to find a Pair which has maximum score*/

importjavafx.util.Pair;

importjava.util.ArrayList;

classTest

{

/* This method returns a Pair which hasmaximum score*/

publicstaticPair <String,Integer>

getMaximum(ArrayList < Pair <String,Integer> > l)

{

// Assign minimum value initially

intmax = Integer.MIN_VALUE;

// Pair to store the maximum marks of a

// student with its name

Pair <String, Integer> ans =

newPair <String, Integer> ("", 0);

// Using for each loop to iterate array of

// Pair Objects

for(Pair <String,Integer> temp : l)

{

// Get the score of Student

intval = temp.getValue();

// Check if it is greater than the previous

// maximum marks

if(val > max)

{

max = val; // update maximum

ans = temp; // update the Pair

}

}

returnans;

}

// Driver method to test above method

publicstaticvoidmain (String[] args)

{

intn = 5;//Number of Students

//Create an Array List

ArrayList <Pair <String,Integer> > l =

newArrayList <Pair <String,Integer> > ();

/* Create pair of name of student with their

corresponding score and insert into the

Arraylist */

l.add(newPair <String,Integer> ("Student A", 90));

l.add(newPair <String,Integer> ("Student B", 54));

l.add(newPair <String,Integer> ("Student C", 99));

l.add(newPair <String,Integer> ("Student D", 88));

l.add(newPair <String,Integer> ("Student E", 89));

// get the Pair which has maximum value

Pair <String,Integer> ans = getMaximum(l);

System.out.println(ans.getKey() + " is top scorer "+

"with score of "+ ans.getValue());

}

}

chevron_right

filter_none

Output :

Student C is top scorer with score of 99

Note: The above program might not run in an online IDE, please use an offline compiler.