在 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.