在Java Pair Class Tuple中获取值

使用该getValueX()方法从Java的Pair Tuple类中的特定索引处获取值。例如,getValue0()。

首先让我们看看使用JavaTuples所需的工具。要在JavaTuples中使用Pair类,您需要导入以下包-

import org.javatuples.Pair;

-下载和运行JavaTuples程序的步骤。如果您正在使用Eclipse IDE在JavaTuples中运行Pair Class,则右键单击Project→Properties→Java Build Path→Add External Jars并上传下载的JavaTuples jar文件。

以下是一个例子-

示例

import org.javatuples.Pair;
public class Demo {
   public static void main(String[] args) {
      Pair < String, String > p1 = Pair.with("One", "Two");
      Pair < String, String > p2 = p1.setAt1("Three");
      System.out.println("Result = " + p2);
      System.out.println("Fetch Value: " + p2.getValue0());
   }
}

输出结果

Result = [One, Three]
Fetch Value: One