logo

Javaのペアクラス

C++ では、 std::ペア これは、値のペアを一緒に保持したい場合に非常に役立ちます。 Java でペアに相当するクラスを探していましたが、ペア クラスは Java 7 まで存在しませんでした。JavaFX 2.2 には、 javafx.util.Pair ペアを格納するために使用できるクラス。によって提供されるパラメータ化されたコンストラクターを使用して、値をペアに保存する必要があります。 javafx.util.Pair クラス。

注記: このペアは HashMap/TreeMap で使用されることに注意してください。ここでは、単に一緒に格納される値のペアを指します。



javafx.util.Pairクラスが提供するメソッド

構文: Javaメソッドのペアクラス

Pair var_name = new Pair(key, value);>
  • ペア (K キー、V 値): 新しいペアを作成します。
  • ブール値等しい(): 2 つのオブジェクトのペアを比較するために使用されます。これは詳細な比較を行います。つまり、ペア オブジェクトに格納されている値 () に基づいて比較します。

例:

ジャワ



決定論的有限オートマトン






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

>

分数として .04
>

出力:

true false>
  • 文字列 toString(): このメソッドは、ペアの文字列表現を返します。
  • K getKey(): ペアのキーを返します。
  • V getValue(): ペアの値を返します。
  • int ハッシュコード(): ペアのハッシュ コードを生成します。

値へのアクセス: 使用する getKey() そして getValue() メソッドを使用して、Pair オブジェクトの値にアクセスできます。

1. getKey(): 最初の値を取得します。
2. getValue(): 2 番目の値を取得します

注記: ここで、 は一緒に格納される値のペアを指します。 Mapで使われるペアとは違います。

実装:

ジャワ




// 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());> >}> }>

スイッチケースJava
>

>

次の問題を見てみましょう。

問題文 : n 人の生徒の名前と、クイズで得られた対応するスコアが与えられます。クラス内で最高点の生徒を見つける必要があります。

注記: 以下のプログラムを実行するには、マシンに Java 8 がインストールされている必要があります。

ジャワ




// 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 = 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());> >}> }>

>

>

文字列から整数へ

出力:

Student C is top scorer with score of 99>

注記: 上記のプログラムはオンライン IDE では実行できない可能性があります。オフライン コンパイラを使用してください。