Explore Pair Class in Java With Techniques

1. Objective

After Jar file in Java tutorial, we move towards the pair class in Java and how to use a pair in Java. Also, we will see various aspects of Java 8 Pair Class. At last, we will discuss the Java Pair Example.

So, let us start Pair Class in Java.

Pair Class in Java

Explore Pair Class in Java With Techniques

2. Pair Class in Java

In C++, we have std::pair in the utility library which is of monstrous utilize on the off chance that we need to keep a couple of qualities together. We were searching for an equal class for a match in Java yet Pair class did not appear till Java 7. JavaFX 2.2 has the javafx.util.Pair class which can utilize to store a couple. We have to store the qualities into Pair utilizing the parameterized constructor gave by the javafx.util.Pair class.

Do you know What is Java Class?

Note that the <Key, Value> match utilized as a part of HashMap/TreeMap. Here, <Key, Value> essentially alludes to a couple of qualities that are put away together.

3. Techniques Given by the javafx.util.Pair Class

This are the techniques given by Pair Class in Java, let’s discuss them one by one:

i. Match (K key, V value): Creates another new pair

ii. Boolean equivalents(): It is utilized to look at two sets of objects. It completes a profound examination, i.e., it looks at on the fundamental of the qualities (<Key, Value>) which are put away in the match objects.

Example –

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));

Read about Java Character Class Methods with Syntax and Examples

iii. String toString(): Returns the String representation of the Pair.

iv. K getKey(): Returns key for the pair.

v. V getValue(): Returns value for the pair.

vi. int hashCode(): This method generates a hash code for the Pair.

Java Pair Class Example –

import javafx.util.Pair;
import java.util.ArrayList;
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; // update maximum
ans = temp; // update the Pair
}
}
return ans;
}
public static void main (String[] args)
{
int n = 5;//Number of Students
//Create an Array List
ArrayList > l =
new ArrayList > ();
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());
}
}

This was all about Pair Class in Java Tutorial. Hope you like our explanation.

Let’s Explore Java File Class – java.io.File Class in Java

4. Conclusion

Hence, in this tutorial, we learned about what is pair class in Java, various techniques associated with java pair class. At last, we saw an example of Pair class in Java. Furthermore, if you have any query feel free to ask in the comment section.

Related Topic- Difference Between Abstract Class and Interface in Java

For reference 

Leave a Reply

Your email address will not be published. Required fields are marked *