📜  JavaTuples-迭代

📅  最后修改于: 2020-11-15 03:26:19             🧑  作者: Mango


每个元组都实现Iterable接口,并且可以类似于collection的方式进行迭代。

Pair pair = Pair.with("Test", Integer.valueOf(5)); 
for(Object object: Pair){
    System.out.println(object);
}

让我们看看JavaTuples的作用。在这里,我们将看到如何迭代元组。

C:\> JavaTuples中创建一个名为TupleTester的Java类文件。

文件:TupleTester.java

package com.tutorialspoint;
import org.javatuples.Quartet;
import org.javatuples.Triplet;
public class TupleTester {
   public static void main(String args[]){
      Triplet triplet = Triplet.with(
         "Test1", Integer.valueOf(5), "Test2"
      );
      for(Object object: triplet) {
         System.out.print(object + " " );
      }
      System.out.println();
      System.out.println(triplet);
      String[] strArray = new String[] {"a", "b" , "c" , "d"};
      Quartet quartet = Quartet.fromArray(strArray);
      for(Object object: quartet) {
         System.out.print(object + " " );
      }
      System.out.println();
      System.out.println("Quartet:" + quartet);
   }
}

验证结果

使用javac编译器编译类,如下所示:

C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java

现在运行TupleTester以查看结果-

C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester

输出

验证输出

Test1 5 Test2 
[Test1, 5, Test2]
a b c d 
Quartet:[a, b, c, d]