JavaTuples – Octet Class ”; Previous Next Introduction The org.javatuples.Octet class represents a Tuple with eight elements. Class Declaration Following is the declaration for org.javatuples.Octet class − public final class Octet<A, B, C, D, E, F, G, H> extends Tuple implements IValue0<A>, IValue1<B>, IValue2<C>, IValue3<D>, IValue4<E>, IValue5<F>, IValue6<G>, IValue7<H> Class Constructor Sr.No. Constructor & Description 1 Octet(A value0, B value1, C value2, D value3, E value4, F value5, G value6, H value7) This creates a Octet Tuple. Class Methods Sr.No. Method & Description 1 Ennead add(Unit tuple) This method returns a Ennead tuple. Similarly other methods to add tuples are available e.g. add(Pair tuple) returns Decade. 2 Ennead add(X0 value) This method add a value to the tuple and returns a Ennead tuple. Similarly other methods to add values are available e.g. add(X0 value0, X1 value1) returns Decade. 3 Ennead addAt0(Unit value) This method add a Unit tuple at index 0 and returns a Ennead tuple. Similarly other methods to add tuples are available e.g. addAt0(Pair value) returns Decade. Other similar method are addAt1(Unit value) which add a unit at index0 and have similar methods upto addAt7(Pair). 4 Ennead addAt0(X0 value) This method add a value at index 0 and returns a Ennead tuple. Similarly other methods to add values are available e.g. addAt0(X0 value0, X1 value1) returns Decade. Other similar method are addAt1(X0 value) which add a value at index0 and have similar methods upto addAt7() with two parameters. 5 static <X> Octet<X,X,X,X,X,X,X,X> fromArray(X[] array) Create tuple from array. 6 static <X> Octet<X,X,X,X,X,X,X,X> fromCollection(Collection<X> collection) Create tuple from collection. 7 static <X> Octet<X,X,X,X,X,X,X,X> fromIterable(Iterable<X> iterable) Create tuple from iterable. 8 static <X> Octet<X,X,X,X,X,X,X,X> fromIterable(Iterable<X> iterable, int index) Create tuple from iterable, starting from the specified index. 9 int getSize() Return the size of the tuple. 10 A getValue0() Returns the value of the tuple at index 0. Similarly getValue1() upto getValue7() returns the value at index 1 and so on. 11 Septet<B,C,D,E,F,G,H> removeFrom0() Return the tuple after removing value of the tuple at index 0. Similarly removeFrom1() upto removeFrom7() returns the tuple after removing value of the tuple at index 1 and so on. 12 <X> Octet<X,B,C,D,E,F,G,H> setAt0(X value) Set the value of the tuple at index 0. Similarly setAt1() upto setAt7() set the value at index 1, and so on. 13 static <A> Octet<A,B,C,D,E,F,G,H> with(A value0, B value1, C value2, D value3, E value4, F value5, G value6, H value7) Create the tuple using given value. Methods inherite This class inherits methods from the following classes − org.javatuples.Tuple Object Example Let”s see Octet Class in action. Here we”ll see how to use various methods. Create a java class file named TupleTester in C:>JavaTuples. File: TupleTester.java package com.tutorialspoint; import java.util.ArrayList; import java.util.List; import org.javatuples.Ennead; import org.javatuples.Octet; import org.javatuples.Septet; public class TupleTester { public static void main(String args[]){ Octet<Integer, Integer, Integer, Integer, Integer,Integer,Integer,Integer> octet = Octet.with(5, 6, 7,8,9,10,11,12); System.out.println(octet); boolean isPresent = octet.contains(5); System.out.println(“5 is present: ” + isPresent); List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); list.add(6); list.add(7); list.add(8); Ennead<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, String> ennead = octet.add(“Test”); System.out.println(ennead); Integer value = octet.getValue0(); System.out.println(value); Septet<Integer, Integer, Integer, Integer,Integer, Integer,Integer> septet = octet.removeFrom0(); System.out.println(septet); Octet<Integer, Integer, Integer, Integer, Integer,Integer, Integer, Integer> octet1 = Octet.fromCollection(list); System.out.println(octet1); } } 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 [5, 6, 7, 8, 9, 10, 11, 12] 5 is present: true [5, 6, 7, 8, 9, 10, 11, 12, Test] 5 [6, 7, 8, 9, 10, 11, 12] [1, 2, 3, 4, 5, 6, 7, 8] Print Page Previous Next Advertisements ”;
Category: javatuples
JavaTuples – Iteration
JavaTuples – Iteration ”; Previous Next Each tuple implements Iterable interface and can be iterated in similar fashion as collection. Pair<String, Integer> pair = Pair.with(“Test”, Integer.valueOf(5)); for(Object object: Pair){ System.out.println(object); } Example Let”s see JavaTuples in action. Here we”ll see how to iterate tuples. Create a java class file named TupleTester in 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 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 Test1 5 Test2 [Test1, 5, Test2] a b c d Quartet:[a, b, c, d] Print Page Previous Next Advertisements ”;
JavaTuples – Quick Guide
JavaTuples – Quick Guide ”; Previous Next JavaTuples – Overview Tuple Tuple is a sequence of objects which may or may not be of same type. Consider the following example − [12,”TutorialsPoint”, java.sql.Connection@li757b] Above object is a tuple of three elements, an Integer, a string and a Connection Object. JavaTuple JavaTuples is a very simple library which offers ten different tuple classses which are sufficient to handle most of the tuple related requirements. Unit<A> – 1 element Pair<A,B> – 2 elements Triplet<A,B,C> – 3 elements Quartet<A,B,C,D> – 4 elements Quintet<A,B,C,D,E> – 5 elements Sextet<A,B,C,D,E,F> – 6 elements Septet<A,B,C,D,E,F,G> – 7 elements Octet<A,B,C,D,E,F,G,H> – 8 elements Ennead<A,B,C,D,E,F,G,H,I> – 9 elements Decade<A,B,C,D,E,F,G,H,I,J> – 10 elements Apart from these tuple classes, JavaTuples also provides two additional classes for semantics sake. KeyValue<A,B> LabelValue<A,B> All tuple classes are typesafe and immutable and implements following interfaces and methods. Iterable Serializable Comparable<Tuple> equals() hashCode() toString() Tuple vs List/Array List or Array can contain any number of elements but each element must be of same type whereas tuples can contain only specific number of elements, can have different type of elements but still are typesafe. JavaTuples – Environment Setup Local Environment Setup If you are still willing to set up your environment for Java programming language, then this section guides you on how to download and set up Java on your machine. Please follow the steps mentioned below to set up the environment. Java SE is freely available from the link Download Java. So you download a version based on your operating system. Follow the instructions to download Java and run the .exe to install Java on your machine. Once you have installed Java on your machine, you would need to set environment variables to point to correct installation directories − Setting up the Path for Windows 2000/XP We are assuming that you have installed Java in c:Program Filesjavajdk directory − Right-click on ”My Computer” and select ”Properties”. Click on the ”Environment variables” button under the ”Advanced” tab. Now, alter the ”Path” variable so that it also contains the path to the Java executable. Example, if the path is currently set to ”C:WINDOWSSYSTEM32”, then change your path to read ”C:WINDOWSSYSTEM32;c:Program Filesjavajdkbin”. Setting up the Path for Windows 95/98/M We are assuming that you have installed Java in c:Program Filesjavajdk directory − Edit the ”C:autoexec.bat” file and add the following line at the end − ”SET PATH=%PATH%;C:Program Filesjavajdkbin” Setting up the Path for Linux, UNIX, Solaris, FreeBS Environment variable PATH should be set to point to where the Java binaries have been installed. Refer to your shell documentation if you have trouble doing this. Example, if you use bash as your shell, then you would add the following line to the end of your ”.bashrc: export PATH=/path/to/java:$PATH” Popular Java Editor To write your Java programs, you need a text editor. There are many sophisticated IDEs available in the market. But for now, you can consider one of the following − Notepad − On Windows machine you can use any simple text editor like Notepad (Recommended for this tutorial), TextPad. Netbeans − It is a Java IDE that is open-source and free which can be downloaded from www.netbeans.org/index.html. Eclipse − It is also a Java IDE developed by the eclipse open-source community and can be downloaded from www.eclipse.org. Download JavaTuples Archie Download the latest version of JavaTuples jar file from Maven Repository – JavaTuples. In this tutorial, javatuples-1.2.jar is downloaded and copied into C:> javatuples folder. OS Archive name Windows javatuples-1.2.jar Linux javatuples-1.2.jar Mac javatuples-1.2.jar Set JavaTuples Environment Set the JavaTuples environment variable to point to the base directory location where JavaTuples jar is stored on your machine. Assuming, we”ve extracted javatuples-1.2.jar in JavaTuples folder on various Operating Systems as follows. OS Output Windows Set the environment variable JavaTuples to C:JavaTuples Linux export JavaTuples=/usr/local/JavaTuples Mac export JavaTuples=/Library/JavaTuples Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the JavaTuples jar location. Assuming, you have stored javatuples-1.2.jar in JavaTuples folder on various Operating Systems as follows. OS Output Windows Set the environment variable CLASSPATH to %CLASSPATH%;%JavaTuples%javatuples-1.2.jar;.; Linux export CLASSPATH=$CLASSPATH:$JavaTuples/javatuples-1.2.jar:. Mac export CLASSPATH=$CLASSPATH:$JavaTuples/javatuples-1.2.jar:. JavaTuples – Create Tuples A tuple using JavaTuple classes can be created using multiple options. Following are the examples − Using with() Methods Each tuple class has a with() method with corresponding parameters. For example − Pair<String, Integer> pair = Pair.with(“Test”, Integer.valueOf(5)); Triplet<String, Integer, Double> triplet = Triplet.with(“Test”, Integer.valueOf(5), Double.valueOf(32.1)); Using Constructor Each tuple class has a constructor with corresponding parameters. For example − Pair<String, Integer> pair = new Pair(“Test”, Integer.valueOf(5)); Triplet<String, Integer, Double> triplet = new Triplet(“Test”, Integer.valueOf(5), Double.valueOf(32.1)); Using Collections Each tuple class has a fromCollection() method with corresponding parameters. For example − Pair<String, Integer> pair = Pair.fromCollection(listOfTwoElements); Using Iterable Each tuple class has a fromIterable() method to get elements in generic fashion. For example − // Retrieve three values from an iterable starting at index 5 Triplet<Integer,Integer,Integer> triplet = Triplet.fromIterable(listOfInts, 5); Example Let”s see JavaTuples in action. Here we”ll see how to create tupels using various ways. Create a java class file named TupleTester in C:>JavaTuples. File: TupleTester.java package com.tutorialspoint; import java.util.ArrayList; import java.util.List; import org.javatuples.Pair; public class TupleTester { public static void main(String args[]){ //Create using with() method Pair<String, Integer> pair = Pair.with(“Test”, Integer.valueOf(5)); //Create using constructor() Pair<String, Integer> pair1 = new Pair(“Test”, Integer.valueOf(5)); List<Integer> listOfInts = new ArrayList<Integer>(); listOfInts.add(1); listOfInts.add(2); //Create using fromCollection() method Pair<Integer, Integer> pair2 = Pair.fromCollection(listOfInts); listOfInts.add(3); listOfInts.add(4); listOfInts.add(5); listOfInts.add(6); listOfInts.add(8); listOfInts.add(9); listOfInts.add(10); listOfInts.add(11); //Create using fromIterable() method // Retrieve three values from an iterable starting at index 5 Pair<Integer,Integer> pair3 = Pair.fromIterable(listOfInts, 5); //print all tuples System.out.println(pair); System.out.println(pair1); System.out.println(pair2); System.out.println(pair3); } } 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 [Test, 5] [Test, 5] [1, 2] [6, 8] JavaTuples – Get Values A tuple has getValueX() methods to get values and getValue() a generic method to get value by index.
JavaTuples – Useful Resources ”; Previous Next The following resources contain additional information on JavaTuples. Please use them to get more in-depth knowledge on this. Useful Links on JavaTuple JavaTuples − JavaTuples Official Home page. Useful Books on JavaTuple To enlist your site on this page, please drop an email to [email protected] Print Page Previous Next Advertisements ”;
JavaTuples – Sextet Class
JavaTuples – Sextet Class ”; Previous Next Introduction The org.javatuples.Sextet class represents a Tuple with six elements. Class Declaration Following is the declaration for org.javatuples.Sextet class − public final class Sextet<A, B, C, D, E, F> extends Tuple implements IValue0<A>, IValue1<B>, IValue2<C>, IValue3<D>, IValue4<E>, IValue5<F> Class Constructor Sr.No. Constructor & Description 1 Sextet(A value0, B value1, C value2, D value3, E value4, F value5) This creates a Sextet Tuple. Class Methods Sr.No. Method & Description 1 Septet add(Unit tuple) This method returns a Septet tuple. Similarly other methods to add tuples are available e.g. add(Pair tuple) returns Octet and upto add(Quartet tuple) returns Decade tuple. 2 Septet add(X0 value) This method add a value to the tuple and returns a Septet tuple. Similarly other methods to add values are available e.g. add(X0 value0, X1 value1) returns Octet and so on upto add() with four parameters. 3 Septet addAt0(Unit value) This method add a Unit tuple at index 0 and returns a Septet tuple. Similarly other methods to add tuples are available e.g. addAt0(Pair value) returns Octet and so on upto addAt0(Quartet). Other similar method are addAt1(Unit value) which add a unit at index0 and have similar methods upto addAt5(Quartet). 4 Septet addAt0(X0 value) This method add a value at index 0 and returns a Septet tuple. Similarly other methods to add values are available e.g. addAt0(X0 value0, X1 value1) returns Octet and so on upto addAt0() with four parameters. Other similar method are addAt1(X0 value) which add a value at index0 and have similar methods upto addAt5() with four parameters. 5 static <X> Sextet<X,X,X,X,X,X> fromArray(X[] array) Create tuple from array. 6 static <X> Sextet<X,X,X,X,X,X> fromCollection(Collection<X> collection) Create tuple from collection. 7 static <X> Sextet<X,X,X,X,X,X> fromIterable(Iterable<X> iterable) Create tuple from iterable. 8 static <X> Sextet<X,X,X,X,X,X> fromIterable(Iterable<X> iterable, int index) Create tuple from iterable, starting from the specified index. 9 int getSize() Return the size of the tuple. 10 A getValue0() Returns the value of the tuple at index 0. Similarly getValue1() upto getValue5() returns the value at index 1 and so on. 11 Quintet<B,C,D,E,F> removeFrom0() Return the tuple after removing value of the tuple at index 0. Similarly removeFrom1() upto removeFrom5() returns the tuple after removing value of the tuple at index 1 and so on. 12 <X> Sextet<X,B,C,D,E,F> setAt0(X value) Set the value of the tuple at index 0. Similarly setAt1() upto setAt5() set the value at index 1, and so on. 13 static <A> Sextet<A,B,C,D,E,F> with(A value0, B value1, C value2, D value3, E value4, F value5) Create the tuple using given value. Methods inherite This class inherits methods from the following classes − org.javatuples.Tuple Object Example Let”s see Sextet Class in action. Here we”ll see how to use various methods. Create a java class file named TupleTester in C:>JavaTuples. File: TupleTester.java package com.tutorialspoint; import java.util.ArrayList; import java.util.List; import org.javatuples.Quartet; import org.javatuples.Quintet; import org.javatuples.Sextet; import org.javatuples.Septet; public class TupleTester { public static void main(String args[]){ Sextet<Integer, Integer, Integer, Integer, Integer,Integer> sextet = Sextet.with(5, 6, 7,8,9,10); System.out.println(sextet); boolean isPresent = sextet.contains(5); System.out.println(“5 is present: ” + isPresent); List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); list.add(6); Septet<Integer, Integer, Integer, Integer, Integer, Integer, String> septet = sextet.add(“Test”); System.out.println(septet); Integer value = sextet.getValue0(); System.out.println(value); Quintet<Integer, Integer, Integer, Integer,Integer> quintet = sextet.removeFrom0(); System.out.println(quintet); Sextet<Integer, Integer, Integer, Integer, Integer,Integer> sextet1 = Sextet.fromCollection(list); System.out.println(sextet1); } } 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 [5, 6, 7, 8, 9, 10] 5 is present: true [5, 6, 7, 8, 9, 10, Test] 5 [6, 7, 8, 9, 10] [1, 2, 3, 4, 5, 6] Print Page Previous Next Advertisements ”;
JavaTuples – Decade Class
JavaTuples – Decade Class ”; Previous Next Introduction The org.javatuples.Decade class represents a Tuple with ten elements. Class Declaration Following is the declaration for org.javatuples.Decade class − public final class Decade<A, B, C, D, E, F, G, H, I, J> extends Tuple implements IValue0<A>, IValue1<B>, IValue2<C>, IValue3<D>, IValue4<E>, IValue5<F>, IValue6<G>, IValue7<H>, IValue8<I>, IValue9<J> Class Constructor Sr.No. Constructor & Description 1 Decade(A value0, B value1, C value2, D value3, E value4, F value5, G value6, H value7, I value8, I value9 ) This creates a Decade Tuple. Class Methods Sr.No. Method & Description 1 static <X> Decade<X,X,X,X,X,X,X,X,X,X > fromArray(X[] array) Create tuple from array. 2 static <X> Decade<X,X,X,X,X,X,X,X,X,X> fromCollection(Collection<X> collection) Create tuple from collection. 3 static <X> Decade<X,X,X,X,X,X,X,X,X,X> fromIterable(Iterable<X> iterable) Create tuple from iterable. 4 static <X> Decade<X,X,X,X,X,X,X,X,X> fromIterable(Iterable<X> iterable, int index) Create tuple from iterable, starting from the specified index. 5 int getSize() Return the size of the tuple. 6 A getValue0() Returns the value of the tuple at index 0. Similarly getValue1() upto getValue9() returns the value at index 1 and so on. 7 Ennead<B,C,D,E,F,G,H,I,J> removeFrom0() Return the tuple after removing value of the tuple at index 0. Similarly removeFrom1() upto removeFrom9() returns the tuple after removing value of the tuple at index 1 and so on. 8 <X> Decade<X,B,C,D,E,F,G,H,I,J> setAt0(X value) Set the value of the tuple at index 0. Similarly setAt1() upto setAt9() set the value at index 1, and so on. 9 static <A> Decade<A,B,C,D,E,F,G,H,I,J> with(A value0, B value1, C value2, D value3, E value4, F value5, G value6, H value7, I value8, I value9) Create the tuple using given value. Methods inherite This class inherits methods from the following classes − org.javatuples.Tuple Object Example Let”s see Ennead Class in action. Here we”ll see how to use various methods. Create a java class file named TupleTester in C:>JavaTuples. File: TupleTester.java package com.tutorialspoint; import java.util.ArrayList; import java.util.List; import org.javatuples.Decade; import org.javatuples.Ennead; public class TupleTester { public static void main(String args[]){ Decade<Integer, Integer, Integer, Integer, Integer,Integer,Integer,Integer, Integer, Integer> decade = Decade.with(5, 6, 7,8,9,10,11,12,13,14); System.out.println(decade); boolean isPresent = decade.contains(5); System.out.println(“5 is present: ” + isPresent); List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); list.add(6); list.add(7); list.add(8); list.add(9); list.add(10); Integer value = decade.getValue0(); System.out.println(value); Ennead<Integer, Integer, Integer, Integer,Integer, Integer,Integer, Integer, Integer> ennead = decade.removeFrom0(); System.out.println(ennead); Decade<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer,Integer, Integer> decade1 = Decade.fromCollection(list); System.out.println(decade1); } } 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 [5, 6, 7, 8, 9, 10, 11, 12, 13, 14] 5 is present: true 5 [6, 7, 8, 9, 10, 11, 12, 13, 14] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Print Page Previous Next Advertisements ”;
JavaTuples – Conversion
JavaTuples – Conversion ”; Previous Next Tuple to List/Array A tuple can be converted to List/Array but at cost of type safety and converted list is of type List<Object>/Object[]. List<Object> list = 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(list); 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 list/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; public class TupleTester { public static void main(String args[]){ Triplet<String, Integer, String> triplet = Triplet.with( “Test1”, Integer.valueOf(5), “Test2” ); List<Object> list = triplet.toList(); Object[] array = triplet.toArray(); System.out.println(“Triplet:” + triplet); System.out.println(“List: ” + list); 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] Print Page Previous Next Advertisements ”;
JavaTuples – Add Elements
JavaTuples – Add Elements ”; Previous Next A tuple has add() method at the end of a tuple and it changes the type of tuple as well. For example adding a element to Triplet tuple will convert it to a Quartet tuple. Quartet<String,String,String,String> quartet = triplet.add(“Test”); A tuple has addAtX() methods as well to add a position at particular index starting from 0. Quartet<String,String,String,String> quartet = triplet.addAt1(“Test”); A tuple can add more than one elements using addAtX() methods. Quartet<String,String,String,String> quartet = pair.addAt1(“Test1”, “Test2″); A tuple can add a tuple as well using addAtX() methods. Quartet<String,String,String,String> quartet = pair.addAt1(pair1); Example Let”s see JavaTuples in action. Here we”ll see how to add values in a tuple using various ways. Create a java class file named TupleTester in C:>JavaTuples. File: TupleTester.java package com.tutorialspoint; import org.javatuples.Pair; import org.javatuples.Quartet; import org.javatuples.Quintet; import org.javatuples.Triplet; public class TupleTester { public static void main(String args[]){ Pair<String, Integer> pair = Pair.with(“Test”, Integer.valueOf(5)); Triplet<String, Integer, String> triplet = pair.add(“Test2”); Quartet<String, String, Integer, String> quartet = triplet.addAt1(“Test1”); Quintet<String, Integer, String, String, Integer> quintet = triplet.add(pair); System.out.println(“Pair: ” + pair); System.out.println(“Triplet:” + triplet); System.out.println(“Quartet:” + quartet); System.out.println(“Quintet:” + quintet); } } 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 Pair: [Test, 5] Triplet:[Test, 5, Test2] Quartet:[Test, Test1, 5, Test2] Quintet:[Test, 5, Test2, Test, 5] Print Page Previous Next Advertisements ”;
Implementing Octet using Septet Class ”; Previous Next Problem Description How to implement Octet class using Septet class? Example Following example shows how to accomplish the above task. Each tuple has add() and addAtX() methods to convert the tuple. Create a java class file named TupleTester in C:>JavaTuples. File: TupleTester.java package com.tutorialspoint; import org.javatuples.Octet; import org.javatuples.Septet; public class TupleTester { public static void main(String args[]){ Septet<Integer, Integer, Integer, Integer, Integer, Integer, Integer> septet = Septet.with(5,6,7,8,9,10,11); System.out.println(septet); Octet<Integer, Integer, Integer, Integer, Integer, Integer, Integer, String> octet = septet.add(“test”); Octet<String, Integer, Integer, Integer, Integer, Integer, Integer, Integer> octet1 = septet.addAt0(“test”); System.out.println(octet); System.out.println(octet1); } } 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 [5, 6, 7, 8, 9, 10, 11] [5, 6, 7, 8, 9, 10, 11, test] [test, 5, 6, 7, 8, 9, 10, 11] Print Page Previous Next Advertisements ”;
JavaTuples – Home
JavaTuples Tutorial PDF Version Quick Guide Resources Job Search Discussion JavaTuples is a java library which provides the set of classes to work with tuples where tuple is a sequence of objects which may or may not be of different types. Audience This tutorial has been prepared for the beginners to help them understand the basic functionality of JavaTuples library to use Tuples in Java based programs. Prerequisites For this tutorial, we assume the readers to have prior knowledge of basic software development using Java or any other programming language. It will help if you had some exposure to the software build and deployment process. Print Page Previous Next Advertisements ”;