JavaTuples 元组迭代
精华
小牛编辑
185浏览
2023-03-14
JavaTuples 元组迭代的介绍
每个元组都实现了 Iterable 接口,并且可以以与集合类似的方式进行迭代。
Pair<String, Integer> pair = Pair.with("Test", Integer.valueOf(5));
for(Object object: Pair){
System.out.println(object);
}
JavaTuples 元组迭代的示例
在这里,我们将看到如何迭代元组。
package cn.xnip;
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);
}
}
输出结果为: