Class Pair<Key, Value> – A convenience class to represent name-value pairs. pair stores a key-pair value. Two Pair objects are considered equal if key and value of one pair is matching with second key. This class also generates hash code using key and value. So hash code will also be same for two Pair objects if their keys and values are same.
Constructor Detail:
public Pair(K key, V value)
- Creates a new pair
Parameters:
- key – The key for this pair
- value – The value to use for this pair
Methods and description:
Return Type | Method | Description |
key for this pair | getKey() | Gets the key for this pair. |
value for this pair | getValue() | Gets the value for this pair. |
int (hash code for this Pair) | hashCode() |
|
String | toString() | String representation of this Pair. |
boolean | equals(Object o) |
Example: Pair<String,Integer> p1 = new Pair(1,5); Pair p2 = new Pair(1,5); Pair p3 = new Pair(2,6); System.out.println(p1.equals(p2) + " " + p2.equals(p3)); Output: true false |
Example:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.util.Pair; | |
import java.util.ArrayList; | |
public class PairExample { | |
public static void main(String[] args) { | |
Pair<String, Integer> pair1 = new Pair<>("apple",1); | |
Pair<String, Integer> pair2 = new Pair<>("apple",1); | |
Pair<String, Integer> pair3 = new Pair<>("banana",2); | |
Pair<String, Integer> pair4 = new Pair<>("pineapple",4); | |
ArrayList<Pair<String,Integer>> list = new ArrayList<>(); | |
list.add(pair1); | |
list.add(pair2); | |
list.add(pair3); | |
list.add(pair4); | |
//toString function | |
System.out.println("——–toString function——–"); | |
for(Pair p: list){ | |
System.out.println(p.toString()); | |
} | |
//equals function | |
System.out.println("——–equals function——–"); | |
System.out.println("pair1 and pair2 are equal: " + pair1.equals(pair2)); | |
System.out.println("pair1 and pair3 are equal: " + pair1.equals(pair3)); | |
System.out.println("pair3 and pair4 are equal: " + pair3.equals(pair4)); | |
//getKey and getValue function | |
System.out.println("——–getKey and getValue function——–"); | |
for (Pair p: list){ | |
System.out.println("key: " + p.getKey() + " and its value: " + p.getValue()); | |
} | |
//hashCode function | |
System.out.println("——–hashCode function——–"); | |
for (Pair p: list){ | |
System.out.println("Pair: " + p.toString() + " and hashCode: " + p.hashCode()); | |
} | |
System.out.println(" As you can see that hashcode for pair1 and pair2" + | |
" are same since key and values are equal"); | |
} | |
} |
Output:
--------toString function-------- apple=1 apple=1 banana=2 pineapple=4 --------equals function-------- pair1 and pair2 are equal: true pair1 and pair3 are equal: false pair3 and pair4 are equal: false --------getKey and getValue function-------- key: apple and its value: 1 key: apple and its value: 1 key: banana and its value: 2 key: pineapple and its value: 4 --------hashCode function-------- Pair: apple=1 and hashCode: 1209379731 Pair: apple=1 and hashCode: 1209379731 Pair: banana=2 and hashCode: -972748765 Pair: pineapple=4 and hashCode: -1349826842 As you can see that hashcode for pair1 and pair2 are same since key and values are equal
Reference: wiki