logo

Java의 쌍 클래스

C++에서는 표준::쌍 한 쌍의 값을 함께 유지하려는 경우 매우 유용하게 사용되는 유틸리티 라이브러리입니다. 우리는 Java에서 pair에 해당하는 클래스를 찾고 있었지만 pair 클래스는 Java 7까지 존재하지 않았습니다. JavaFX 2.2에는 javafx.util.쌍 쌍을 저장하는 데 사용할 수 있는 클래스입니다. 에서 제공하는 매개변수화된 생성자를 사용하여 값을 쌍에 저장해야 합니다. javafx.util.쌍 수업.

메모: 이 쌍은 HashMap/TreeMap 에서 사용됩니다. 여기서는 단순히 함께 저장된 값 쌍을 나타냅니다.



javafx.util.Pair 클래스에서 제공하는 메서드

통사론: Java 메소드의 pair 클래스

Pair var_name = new Pair(key, value);>
  • 쌍(K 키, V 값): 새 쌍을 만듭니다.
  • 부울 같음(): 두 쌍의 개체를 비교하는 데 사용됩니다. 즉, 쌍 개체에 저장된 값()을 기준으로 비교합니다.

예:

자바








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

>

>

산출:

true false>
  • 문자열 toString(): 이 메소드는 쌍의 문자열 표현을 반환합니다.
  • K getKey(): 쌍의 키를 반환합니다.
  • V getValue(): 쌍에 대한 값을 반환합니다.
  • 정수 해시코드(): 쌍에 대한 해시 코드를 생성합니다.

값에 액세스: 사용 getKey() 그리고 getValue() 메소드를 사용하면 pair 객체의 값에 접근할 수 있습니다.

1. getKey(): 첫 번째 값을 가져옵니다.
2. getValue(): 두 번째 값을 가져옵니다.

메모: 여기서 는 함께 저장되는 값 쌍을 의미합니다. Map에서 사용되는 pair와는 다릅니다.

구현:

자바




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

bash if 문

>

>

다음 문제를 살펴보겠습니다.

문제 설명 : 퀴즈에서 얻은 점수에 따라 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에서 실행되지 않을 수 있습니다. 오프라인 컴파일러를 사용하십시오.