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 is 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
Syntax: The pair class in the Java method
Pair<Key Type, Value Type> var_name = new Pair<>(key, value);
- Pair (K key, V value): Creates a new pair.
- boolean equals(): It is used to compare two pairs of objects. It does a deep comparison, i.e., it compares on the basis of the values (<Key, Value>) which are stored in the pair objects.
Example:
java
Pair p1 = new Pair( 3 , 4 );
Pair p2 = new Pair( 3 , 4 );
Pair p3 = new Pair( 4 , 4 );
System.out.println(p1.equals(p2) + “ ” + p2.equals(p3));
|
Output:
true false
- String toString(): This method will return the String representation of the Pair.
- K getKey(): It returns the key for the pair.
- V getValue(): It returns a value for the pair.
- int hashCode(): Generate a hash code for the Pair.
Accessing values: Using getKey() and getValue() methods we can access a Pair object’s values.
1. getKey(): gets the first value.
2. getValue(): gets the second value
Note: Here, <Key, Value> refers to a pair of values that are stored together. It is not like <Key, Value> pair which is used in Map.
Implementation:
Java
import javafx.util.Pair;
class GFG {
public static void main(String[] args)
{
Pair<Integer, String> p
= new Pair<Integer, String>( 10 , "Hello Geeks!" );
System.out.println( "The First value is :"
+ p.getKey());
System.out.println( "The Second value is :"
+ p.getValue());
}
}
|
Let us have a look at the following problem.
Problem Statement: We are given the names of n students with their corresponding scores obtained in a quiz. We need to find the student with the maximum score in the class.
Note: You need to have Java 8 installed on your machine in order to run the below program.
Java
import java.util.ArrayList;
import javafx.util.Pair;
class Test {
public static Pair<String, Integer>
getMaximum(ArrayList<Pair<String, Integer> > l)
{
int max = Integer.MIN_VALUE;
Pair<String, Integer> ans
= new Pair<String, Integer>( "" , 0 );
for (Pair<String, Integer> temp : l) {
int val = temp.getValue();
if (val > max) {
max = val;
ans = temp;
}
}
return ans;
}
public static void main(String[] args)
{
int n = 5 ;
ArrayList<Pair<String, Integer> > l
= new ArrayList<Pair<String, Integer> >();
l.add( new Pair<String, Integer>( "Student A" , 90 ));
l.add( new Pair<String, Integer>( "Student B" , 54 ));
l.add( new Pair<String, Integer>( "Student C" , 99 ));
l.add( new Pair<String, Integer>( "Student D" , 88 ));
l.add( new Pair<String, Integer>( "Student E" , 89 ));
Pair<String, Integer> ans = getMaximum(l);
System.out.println(ans.getKey() + " is top scorer "
+ "with score of "
+ ans.getValue());
}
}
|
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.
Last Updated :
25 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...