JavaTuples - Iterasi

Setiap tupel mengimplementasikan antarmuka Iterable dan dapat diiterasi dengan cara yang sama seperti koleksi.

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

Contoh

Mari kita lihat JavaTuple beraksi. Di sini kita akan melihat bagaimana mengulang tupel.

Buat file kelas java bernama TupleTester di C:\>JavaTuples.

File: 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

Kompilasi kelas menggunakan javac kompiler sebagai berikut -

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

Sekarang jalankan TupleTester untuk melihat hasilnya -

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

Keluaran

Verifikasi Output

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