在 Java 中迭代 KeyValue 元组


要迭代 Java 中的 KeyValue 元组,请使用 Iterable 并使用 for 循环对其进行迭代。让我们首先看看在 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("Iterating through the values of the KeyValue Tuple = ");
      for (Object ele : kValue) System.out.println(ele);
      boolean res = kValue.contains("Rank");
      System.out.println("Does the value exist in the Tuple? = "+res);
   }
}

输出

Iterating through the values of the KeyValue Tuple =
77
Rank
Does the value exist in the Tuple? = true

更新日期: 30-Jul-2019

391 浏览量

开启你的 职业生涯

完成该课程并获得认证

开始行动
广告
© . All rights reserved.