Java KeyValue 元組的 contains() 方法


使用 contains() 方法搜尋 KeyValue 元組中的值。首先,我們來看看使用 JavaTuples 需要做些什麼。要在 JavaTuples 中使用 KeyValue 類,需要匯入以下包。

import org.javatuples.KeyValue;

注意 下載 JavaTuples Jar 庫以執行 JavaTuples 程式。如果你使用的是 Eclipse IDE,則右鍵單擊專案 -> 屬性 -> Java 構建路徑 -> 新增外部 Jar 並上傳下載的 JavaTuples jar 檔案。有關執行 JavaTuples 的所有步驟,請參考以下指南。

步驟: 如何在 Eclipse 中執行 JavaTuples 程式

以下是一個示例,在 Java 中實現 KeyValue 元組的 contains() 方法。

示例

import org.javatuples.KeyValue;
public class Demo {
   public static void main(String[] args) {
      KeyValue<Integer, String> kValue = KeyValue.with(77, "Rank");
      System.out.println("Key-Value in the KeyValue Tuple = "+kValue);
      boolean res = kValue.contains("Rank");
      System.out.println("Does the value exist in the Tuple? = "+res);
   }
}

輸出

Key-Value in the KeyValue Tuple = [77, Rank]
Does the value exist in the Tuple? = true

更新於:2019-07-30

84 瀏覽量

開啟你的 職業生涯

完成課程以獲取認證

開始
廣告
© . All rights reserved.