JavaTuples 中的 KeyValue 類是什麼?


KeyValue 類是 2 個元素的元組,即鍵和值。它位於 JavaTuples 庫中。以下是宣告 -

public final class KeyValue<P, Q>
extends Tuple
implements IValueKey<P>, IValueValue<Q>

首先讓我們看看使用 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> value = new KeyValue<Integer, String>(5, "Points");
      System.out.println("Elements in the KeyValue Tuple = "+value);
   }
}

輸出

The following is the output:
Elements in the KeyValue Tuple = [5, Points]

更新於: 2019-07-30

71 次瀏覽

開始您的職業生涯

透過完成課程獲取認證

開始
廣告
© . All rights reserved.