在 Java KeyValue 元組中搜索一個值


要搜尋 KeyValue 元組中的值,請使用 contains() 方法。此處,要搜尋的值應設定為該方法的引數。返回值是一個布林值,即 TRUE(如果該值存在)或 FALSE(如果該值不存在)。讓我們先了解需要使用 JavaTuples 的內容。要使用 JavaTuples 中的 KeyValue 類,您需要匯入以下包

import org.javatuples.KeyValue;

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

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

以下是 Java 中搜索 KeyValue 元組中值的一個示例

示例

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("Points");
      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? = false

更新於: 30-Jul-2019

79 次瀏覽

開啟你的 職業生涯

完成課程即可獲得認證

開始學習
廣告