Implementing Ennead Using Octet Class ”; Previous Next Problem Description How to implement Ennead class using Octet 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.Ennead; import org.javatuples.Octet; 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); Ennead<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, String> ennead = octet.add(“test”); Ennead<String, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer> ennead1 = octet.addAt0(“test”); System.out.println(ennead); System.out.println(ennead1); } } 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, 6, 7, 8, 9, 10, 11, 12, test] [test, 5, 6, 7, 8, 9, 10, 11, 12] Print Page Previous Next Advertisements ”;
Category: javatuples
Implementing Triplet Using Pair Class ”; Previous Next Problem Description How to implement Triplet class using Pair 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.Triplet; import org.javatuples.Pair; public class TupleTester { public static void main(String args[]){ Pair<Integer, Integer> pair = Pair.with(5,6); System.out.println(pair); Triplet<Integer, Integer, String> triplet = pair.add(“test”); Triplet<String, Integer, Integer> triplet1 = pair.addAt0(“test”); System.out.println(triplet); System.out.println(triplet1); } } 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] [5, 6, test] [test, 5, 6] Print Page Previous Next Advertisements ”;
Implementing Quintet Using Quartet Class ”; Previous Next Problem Description How to implement Quintet class using Quartet 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.Quintet; import org.javatuples.Quartet; public class TupleTester { public static void main(String args[]){ Quartet<Integer, Integer, Integer, Integer> quartet = Quartet.with(5,6,7,8); System.out.println(quartet); Quintet<Integer, Integer, Integer, Integer, String> quintet = quartet.add(“test”); Quintet<String, Integer, Integer, Integer, Integer> quintet1 = quartet.addAt0(“test”); System.out.println(quintet); System.out.println(quintet1); } } 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] [5, 6, 7, 8, test] [test, 5, 6, 7, 8] Print Page Previous Next Advertisements ”;
JavaTuples – Remove Elements
JavaTuples – Remove Elements ”; Previous Next A tuple has removeAtX() methods to remove value at particular index. For example Triplet class has following methods. removeAt0() − remove value at index 0 and return the resulted tuple. removeAt1() − remove value at index 1 and return the resulted tuple. removeAt2() − remove value at index 2 and return the resulted tuple. Removing an element returns a new tuple. Example Let”s see JavaTuples in action. Here we”ll see how to remove value in a tuple. Create a java class file named TupleTester in C:>JavaTuples. File: TupleTester.java package com.tutorialspoint; import org.javatuples.Pair; 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” ); Pair<String, Integer> pair = triplet.removeFrom2(); System.out.println(“Triplet:” + triplet); System.out.println(“Pair: ” + pair); } } 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] Pair: [Test1, 5] Print Page Previous Next Advertisements ”;
JavaTuples – Pair Class
JavaTuples – Pair Class ”; Previous Next Introduction The org.javatuples.Pair class represents a Tuple with two elements. Class Declaration Following is the declaration for org.javatuples.Pair class − public final class Pair<A,B> extends Tuple implements IValue0<A>, IValue1<B> Class Constructor Sr.No. Constructor & Description 1 Pair(A value0, B value1) This creates a Pair Tuple. Class Methods Sr.No. Method & Description 1 Triplet add(Unit tuple) This method returns a Triplet tuple. Similarly other methods to add tuples are available e.g. add(Pair tuple) returns Quartet and upto add(Octet tuple) returns Decade tuple. 2 Triplet add(X0 value) This method add a value to the tuple and returns a Triplet tuple. Similarly other methods to add values are available e.g. add(X0 value0, X1 value1) returns Quartet and so on upto add() with eight parameters. 3 Triplet addAt0(Unit value) This method add a Unit tuple at index 0 and returns a Triplet tuple. Similarly other methods to add tuples are available e.g. addAt0(Pair value) returns Quartet and so on upto addAt0(Octet). Other similar method are addAt1(Unit value) which add a unit at index0 and have similar methods upto addAt2(Octet). 4 Triplet addAt0(X0 value) This method add a value at index 0 and returns a Triplet tuple. Similarly other methods to add values are available e.g. addAt0(X0 value0, X1 value1) returns Quartet and so on upto addAt0() with eight parameters. Other similar method are addAt1(X0 value) which add a value at index0 and have similar methods upto addAt2() with eight parameters. 5 static <X> Pair<X,X> fromArray(X[] array) Create tuple from array. 6 static <X> Pair<X,X> fromCollection(Collection<X> collection) Create tuple from collection. 7 static <X> Pair<X,X> fromIterable(Iterable<X> iterable) Create tuple from iterable. 8 static <X> Pair<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() returns the value at index 1. 11 Unit<B> removeFrom0() Return the tuple after removing value of the tuple at index 0. Similarly removeFrom1() returns the tuple after removing value of the tuple at index 1. 12 <X> Pair<X,B> setAt0(X value) Set the value of the tuple at index 0. Similarly setAt1() set the value at index 1. 13 static <A,B> Pair<A,B> with(A value0, B value1) Create the tuple using given value. Methods inherite This class inherits methods from the following classes − org.javatuples.Tuple Object Example Let”s see Pair 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.Pair; import org.javatuples.Triplet; import org.javatuples.Unit; public class TupleTester { public static void main(String args[]){ Pair<Integer, Integer> pair = Pair.with(5,6); System.out.println(pair); boolean isPresent = pair.contains(5); System.out.println(“5 is present: ” + isPresent); List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); Triplet<Integer,Integer, String> triplet = pair.add(“Test”); System.out.println(triplet); Integer value = pair.getValue0(); System.out.println(value); Unit<Integer> unit = pair.removeFrom0(); System.out.println(unit); Pair<Integer, Integer> pair1 = Pair.fromCollection(list); System.out.println(pair1); } } 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] 5 is present: true [5, 6, Test] 5 [6] [1, 2] Print Page Previous Next Advertisements ”;
JavaTuples – Get Values
JavaTuples – Get Values ”; Previous Next A tuple has getValueX() methods to get values and getValue() a generic method to get value by index. For example Triplet class has following methods. getValue(index) − returns value at index starting from 0. getValue0() − returns value at index 0. getValue1() − returns value at index 1. getValue2() − returns value at index 2. Feature getValueX() methods are typesafe and no cast is required, but getValue(index) is generic. A tuple has getValueX() methods upto element count. For example, Triplet has no getValue3() method but Quartet has. Semantic Classes KeyValue and LabelValue has getKey()/getValue() and getLabel()/getValue() instead of getValue0()/getValue1() methods. Example Let”s see JavaTuples in action. Here we”ll see how to get values from a tuple using various ways. Create a java class file named TupleTester in C:>JavaTuples. File: TupleTester.java package com.tutorialspoint; import org.javatuples.KeyValue; 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)); Object value0Obj = pair.getValue(0); Object value1Obj = pair.getValue(1); String value0 = pair.getValue0(); Integer value1 = pair.getValue1(); System.out.println(value0Obj); System.out.println(value1Obj); System.out.println(value0); System.out.println(value1); KeyValue<String, Integer> keyValue = KeyValue.with( “Test”, Integer.valueOf(5) ); value0 = keyValue.getKey(); value1 = keyValue.getValue(); System.out.println(value0Obj); System.out.println(value1Obj); } } 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 Test 5 Print Page Previous Next Advertisements ”;
JavaTuples – Set Values
JavaTuples – Set Values ”; Previous Next A tuple has setAtX() methods to set value at particular index. For example Triplet class has following methods. setAt0() − set value at index 0. setAt1() − set value at index 1. setAt2() − set value at index 2. Feature Tuples are immutable. Each setAtX() returns a new tuple which is to be used to see the updated value. Type of a position of a tuple can be changed using setAtX() method. Example Let”s see JavaTuples in action. Here we”ll see how to set 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; public class TupleTester { public static void main(String args[]){ //Create using with() method Pair<String, Integer> pair = Pair.with(“Test”, Integer.valueOf(5)); Pair<String, Integer> pair1 = pair.setAt0(“Updated Value”); System.out.println(“Original Pair: ” + pair); System.out.println(“Updated Pair:” + pair1); Pair<String, String> pair2 = pair.setAt1(“Changed Type”); System.out.println(“Original Pair: ” + pair); System.out.println(“Changed Pair:” + pair2); } } 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 Original Pair: [Test, 5] Updated Pair:[Updated Value, 5] Original Pair: [Test, 5] Changed Pair:[Test, Changed Type] Print Page Previous Next Advertisements ”;
JavaTuples – Environment Setup ”; Previous Next 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:. Print Page Previous Next Advertisements ”;
JavaTuples – Create Tuples
JavaTuples – Create Tuples ”; Previous Next 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] Print Page Previous Next Advertisements ”;
Implementing Sextet Using Quintet Class ”; Previous Next Problem Description How to implement Sextet class using Quintet 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.Quintet; import org.javatuples.Sextet; public class TupleTester { public static void main(String args[]){ Quintet<Integer, Integer, Integer, Integer, Integer> quintet = Quintet.with(5,6,7,8,9); System.out.println(quintet); Sextet<Integer, Integer, Integer, Integer, Integer, String> sextet = quintet.add(“test”); Sextet<String, Integer, Integer, Integer, Integer, Integer> sextet1 = quintet.addAt0(“test”); System.out.println(sextet); 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] [5, 6, 7, 8, 9, test] [test, 5, 6, 7, 8, 9] Print Page Previous Next Advertisements ”;