C++ valodā mums ir std::pair lietderības bibliotēkā, kas ir ļoti noderīga, ja vēlamies saglabāt vērtību pāri kopā. Mēs meklējām līdzvērtīgu klasi Java pārim, taču pāra klase radās tikai Java 7. JavaFX 2.2 ir javafx.util.Pair klase, kuru var izmantot pāra glabāšanai. Mums ir jāsaglabā vērtības pārī, izmantojot parametrizēto konstruktoru, ko nodrošina javafx.util.Pair klasē.
Piezīme: Ņemiet vērā, ka pāris tiek izmantots programmā HashMap/TreeMap . Šeit vienkārši attiecas uz vērtību pāri, kas tiek glabāti kopā.
Metodes, ko nodrošina klase javafx.util.Pair
Sintakse: Pāra klase Java metodē
pavasara zābaku anotācijas
Pair var_name = new Pair(key, value);>
- Pāris (K atslēga, V vērtība): Izveido jaunu pāri.
- Būla ir vienāds (): To izmanto, lai salīdzinātu divus objektu pārus. Tas veic dziļu salīdzinājumu, t.i., salīdzina, pamatojoties uz vērtībām (), kas tiek saglabātas pāra objektos.
Piemērs:
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));> |
>
>
Izvade:
true false>
- String toString(): Šī metode atgriezīs pāra virknes attēlojumu.
- K getKey(): Tas atgriež pāra atslēgu.
- V getValue(): Tas atgriež vērtību pārim.
- int hashCode(): Ģenerējiet jaucējkodu pārim.
Piekļuve vērtībām: Izmantojot getKey() un getValue() metodes, mēs varam piekļūt Pair objekta vērtībām.
1. getKey(): iegūst pirmo vērtību.
2. getValue(): iegūst otro vērtību
Piezīme: Šeit attiecas uz vērtību pāri, kas tiek glabāti kopā. Tas nav kā pāris, kas tiek izmantots kartē.
Īstenošana:
Java
10 ml ir cik daudz
// Java program to implement in-built pair classes> import> javafx.util.Pair;> class> GFG {> >// Main driver method> >public> static> void> main(String[] args)> >{> >Pair p> >=>new> Pair(>10>,>'Hello Geeks!'>);> >// printing the values of key and value pair> >// separately> >System.out.println(>'The First value is :'> >+ p.getKey());> >System.out.println(>'The Second value is :'> >+ p.getValue());> >}> }> |
>
>
Apskatīsim šādu problēmu.
Problēmas paziņojums : Mums ir doti n skolēnu vārdi un atbilstošie rezultāti, kas iegūti viktorīnā. Mums ir jāatrod skolēns ar maksimālo punktu skaitu klasē.
Piezīme: Lai palaistu tālāk norādīto programmu, jūsu datorā ir jābūt instalētai Java 8.
Java
// Java program to find a Pair which has maximum score> // Importing required classes> import> java.util.ArrayList;> import> javafx.util.Pair;> // class> class> Test {> >// This method returns a Pair which hasmaximum score> >public> static> Pair> >getMaximum(ArrayList l)> >{> >// Assign minimum value initially> >int> max = Integer.MIN_VALUE;> >// Pair to store the maximum marks of a> >// student with its name> >Pair ans> >=>new> Pair(>''>,>0>);> >// Using for each loop to iterate array of> >// Pair Objects> >for> (Pair temp : l) {> >// Get the score of Student> >int> val = temp.getValue();> >// Check if it is greater than the previous> >// maximum marks> >if> (val>max) {> >max = val;>// update maximum> >ans = temp;>// update the Pair> >}> >}> >return> ans;> >}> >// Driver method to test above method> >public> static> void> main(String[] args)> >{> >int> n =>5>;>// Number of Students> >// Create an Array List> >ArrayList l> >=>new> ArrayList();> >/* Create pair of name of student with their> >corresponding score and insert into the> >Arraylist */> >l.add(>new> Pair(>'Student A'>,>90>));> >l.add(>new> Pair(>'Student B'>,>54>));> >l.add(>new> Pair(>'Student C'>,>99>));> >l.add(>new> Pair(>'Student D'>,>88>));> >l.add(>new> Pair(>'Student E'>,>89>));> >// get the Pair which has maximum value> >Pair ans = getMaximum(l);> >System.out.println(ans.getKey() +>' is top scorer '> >+>'with score of '> >+ ans.getValue());> >}> }> |
>
>
Izvade:
Student C is top scorer with score of 99>
Piezīme: Iepriekš minētā programma, iespējams, nedarbosies tiešsaistes IDE. Lūdzu, izmantojiet bezsaistes kompilatoru.