當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Pair用法及代碼示例

在 C++ 中,實用程序庫中有std::pair,如果我們想要將一對值保留在一起,它會非常有用。我們在 Java 中尋找一個與pair 等效的類,但是直到Java 7 才出現Pair 類。JavaFX 2.2 具有javafx.util.Pair 類,可用於存儲pair。我們需要使用 javafx.util.Pair 類提供的參數化構造函數將值存儲到 Pair 中。

Note: Note that the <Key, Value> pair is used in HashMap/TreeMap. Here, <Key, Value> simply refers to a pair of values that are stored together.   

javafx.util.Pair 類提供的方法

用法:Java方法中的pair類

Pair<Key Type, Value Type> var_name = new Pair<>(key, value);
  • 對(K 鍵,V 值):創建一個新對。
  • 布爾值equals():它用於比較兩對對象。它進行深度比較,即,它根據存儲在pair對象中的值(<Key, Value>)進行比較。

例子:

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

輸出:

true false
  • 字符串toString():此方法將返回該對的字符串表示形式。
  • KgetKey():它返回該對的 key 。
  • VgetValue():它返回該對的值。
  • int hashCode():生成該對的哈希碼。

訪問值:使用getKey()和getValue()方法我們可以訪問Pair對象的值。

1.getKey():獲取第一個值。
2.getValue():獲取第二個值

Note: Here, <Key, Value> refers to a pair of values that are stored together. It is not like <Key, Value> pair which is used in Map.

執行:

Java


// Java program to implement in-built pair classes
import javafx.util.Pair;
class GFG {
    // Main driver method
    public static void main(String[] args)
    {
        Pair<Integer, String> p
            = new Pair<Integer, String>(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());
    }
}

讓我們看看下麵這個問題。

問題陳述:我們得到 n 個學生的姓名以及他們在測驗中獲得的相應分數。我們需要找到班上得分最高的學生。

Note: You need to have Java 8 installed on your machine in order to run the below program.

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<String, Integer>
    getMaximum(ArrayList<Pair<String, Integer> > l)
    {
        // Assign minimum value initially
        int max = Integer.MIN_VALUE;
        // Pair to store the maximum marks of a
        // student with its name
        Pair<String, Integer> ans
            = new Pair<String, Integer>("", 0);
        // Using for each loop to iterate array of
        // Pair Objects
        for (Pair<String, Integer> 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<Pair<String, Integer> > l
            = new ArrayList<Pair<String, Integer> >();
        /* Create pair of name of student with their
            corresponding score and insert into the
            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));
        // get the Pair which has maximum value
        Pair<String, Integer> 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

Note: The above program might not run in an online IDE, please use an offline compiler.



相關用法


注:本文由純淨天空篩選整理自佚名大神的英文原創作品 Pair Class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。