JavaTuples Tutorial
Selected Reading
- JavaTuples - Discussion
- JavaTuples - Useful Resources
- JavaTuples - Quick Guide
- JavaTuples - Decade using Ennead
- JavaTuples - Ennead using Octet
- JavaTuples - Octet using Septet
- JavaTuples - Septet using Sextet
- JavaTuples - Sextet using Quintet
- JavaTuples - Quintet using Quartet
- JavaTuples - Quartet using Triplet
- JavaTuples - Triplet using Pair
- JavaTuples - Pair using Unit
- JavaTuples - KeyValue Class
- JavaTuples - LabelValues Class
- JavaTuples - Decade Class
- JavaTuples - Ennead Class
- JavaTuples - Octet Class
- JavaTuples - Septet Class
- JavaTuples - Sextet Class
- JavaTuples - Quintet Class
- JavaTuples - Quartet Class
- JavaTuples - Triplet Class
- JavaTuples - Pair Class
- JavaTuples - Unit Class
- JavaTuples - Checking Elements
- JavaTuples - Iteration
- JavaTuples - Conversion
- JavaTuples - Remove Elements
- JavaTuples - Add Elements
- JavaTuples - Set Values
- JavaTuples - Get Values
- JavaTuples - Create Tuples
- JavaTuples - Environment Setup
- JavaTuples - Overview
- JavaTuples - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
JavaTuples - Conversion
JavaTuples - Conversion
Tuple to List/Array
A tuple can be converted to List/Array but at cost of type safety and converted pst is of type List<Object>/Object[].
List<Object> pst = triplet.toList(); Object[] array = triplet.toArray();
Collection/Array to Tuple
A collection can be converted to tuple using fromCollection() method and array can be converted to tuple using fromArray() method.
Pair<String, Integer> pair = Pair.fromCollection(pst); Quartet<String,String,String,String> quartet = Quartet.fromArray(array);
If size of array/collection is different than that of tuple, then IllegalArgumentException will occur.
Exception in thread "main" java.lang.IllegalArgumentException: Array must have exactly 4 elements in order to create a Quartet. Size is 5 at ...
Example
Let s see JavaTuples in action. Here we ll see how to convert tuple to pst/array and vice versa.
Create a java class file named TupleTester in C:>JavaTuples.
File: TupleTester.java
package com.tutorialspoint; import java.util.List; import org.javatuples.Quartet; import org.javatuples.Triplet; pubpc class TupleTester { pubpc static void main(String args[]){ Triplet<String, Integer, String> triplet = Triplet.with( "Test1", Integer.valueOf(5), "Test2" ); List<Object> pst = triplet.toList(); Object[] array = triplet.toArray(); System.out.println("Triplet:" + triplet); System.out.println("List: " + pst); System.out.println(); for(Object object: array) { System.out.print(object + " " ); } System.out.println(); String[] strArray = new String[] {"a", "b" , "c" , "d"}; Quartet<String, String, String, String> quartet = Quartet.fromArray(strArray); System.out.println("Quartet:" + quartet); } }
Verify the result
Compile the classes using javac compiler as follows −
C:JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java
Now run the TupleTester to see the result −
C:JavaTuples>java -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester
Output
Verify the Output
Triplet:[Test1, 5, Test2] List: [Test1, 5, Test2] Test1 5 Test2 Quartet:[a, b, c, d]Advertisements