在 Java 中迭代 Decade 元组

java 8programmingobject oriented programming

要迭代 Decade 元组,请像 Java 中的其他集合一样操作,即使用 for 循环,迭代并显示元素。首先,让我们看看使用 JavaTuples 需要什么。要在 JavaTuples 中使用 Decade 类,您需要导入以下包

import org.javatuples.Decade;

注意:下载 JavaTuples Jar 库以运行 JavaTuples 程序。如果您使用的是 Eclipse IDE,请右键单击"项目"->"属性"->"Java 构建路径"->"添加外部 Jar",然后上传下载的 JavaTuples Jar 文件。请参阅以下指南,了解运行 JavaTuples 的所有步骤:

步骤:如何在 Eclipse 中运行 JavaTuples 程序

以下是在 Java 中迭代 Decade Tuple 的示例

示例

import org.javatuples.Decade;
public class Demo {
   public static void main(String[] args) {
      // 包含 10 个元素的 Tuple
      Decade<String, String, String, String, String, String, String, String, String, String> d =
         Decade.with("AB","CD", "EF","GH", "IJ","KL", "MN","OP", "QR", "ST");
      System.out.println("Iterating the elements in the Decade Tuple = ");
      for (Object ele : d)
         System.out.println(ele);
         boolean res = d.contains("MN");
         System.out.println("Does the value exist in the Decade Tuple? = "+res);
   }
}

输出

Iterating the elements in the Decade Tuple =
AB
CD
EF
GH
IJ
KL
MN
OP
QR
ST
Does the value exist in the Decade Tuple? = true

相关文章