Javatuples 简明教程
JavaTuples - Iteration
每个元组都实现了 Iterable 接口,可以像集合一样迭代。
Pair<String, Integer> pair = Pair.with("Test", Integer.valueOf(5));
for(Object object: Pair){
System.out.println(object);
}
Example
让我们来看看 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<String, Integer, String> 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<String, String, String, String> quartet = Quartet.fromArray(strArray);
for(Object object: quartet) {
System.out.print(object + " " );
}
System.out.println();
System.out.println("Quartet:" + quartet);
}
}
Verify the result
使用以下 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