当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。