在 Java Pair 元组中搜索值


使用 contains() 方法在 JavaTuples Pair 类中搜索值。

让我们先了解在 JavaTuples 中工作时需要什么。要在 JavaTuples 中使用 Pair 类,需要导入以下包 −

import org.javatuples.Pair;

注意下载并运行 JavaTuples 程序的步骤如果使用 Eclipse IDE 在 JavaTuples 中运行 Pair 类,则右键单击**项目 → 属性 → Java 构建路径 → 添加外部 JAR**并上传下载的 JavaTuples jar 文件。

以下是一个示例 −

示例

import org.javatuples.Pair;
public class Demo {
   public static void main(String[] args) {
      Pair < String, String > p = Pair.with("Mobile", "Desktop");
      System.out.println("Pair = " + p);
      boolean res = p.contains("Mobile");
      System.out.println("Is the value in Tuple? = " + res);
   }
}

输出

Pair = [Mobile, Desktop]
Is the value in Tuple? = true

更新于: 2019 年 7 月 30 日

155 次浏览

开启你的 职业生涯

完成课程以获得认证

立即开始
广告
© . All rights reserved.