JavaTuples – Unit Class ”; Previous Next Introduction The org.javatuples.Unit class represents a Tuple with single element. Class declaration Following is the declaration for org.javatuples.Unit class − public final class Unit<A> extends Tuple implements IValue0<A> Class constructors Sr.No. Constructor & Description 1 Unit(A value0) This creates a Unit Tuple. Class Methods Sr.No. Method & Description 1 Pair add(Unit tuple) This method returns a Pair tuple. Similarly other methods to add tuples are available e.g. add(Pair tuple) returns Triplet and upto add(Ennead tuple) returns Decade tuple. 2 Pair add(X0 value) This method add a value to the tuple and returns a Pair tuple. Similarly other methods to add values are available e.g. add(X0 value0, X1 value1) returns Triplet and so on upto add() with nine parameters. 3 Pair addAt0(Unit value) This method add a Unit tuple at index 0 and returns a Pair tuple. Similarly other methods to add tuples are available e.g. addAt0(Pair value) returns Triplet and so on upto addAt0(Ennead). Other similar method are addAt1(Unit value) which add a unit at index0 and have similar methods upto addAt1(Ennead). 4 Pair addAt0(X0 value) This method add a value at index 0 and returns a Pair tuple. Similarly other methods to add values are available e.g. addAt0(X0 value0, X1 value1) returns Triplet and so on upto addAt0() with nine parameters. Other similar method are addAt1(X0 value) which add a value at index0 and have similar methods upto addAt1() with nine parameters. 5 static <X> Unit<X> fromArray(X[] array) Create tuple from array. 6 static <X> Unit<X> fromCollection(Collection<X> collection) Create tuple from collection. 7 static <X> Unit<X> fromIterable(Iterable<X> iterable) Create tuple from iterable. 8 static <X> Unit<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() Return the value of the tuple. 11 <X> Unit<X> setAt0(X value) Set the value of the tuple. 12 static <A> Unit<A> with(A value0) Create the tuple using given value. Methods inherits This class inherits methods from the following classes − org.javatuples.Tuple Object Example Let”s see Unit 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.Unit; public class TupleTester { public static void main(String args[]){ Unit<Integer> unit = Unit.with(5); System.out.println(unit); boolean isPresent = unit.contains(5); System.out.println(“5 is present: ” + isPresent); List<Integer> list = new ArrayList<>(); list.add(1); Pair<Integer, String> pair = unit.add(“Test”); System.out.println(pair); Integer value = unit.getValue0(); System.out.println(value); Unit<Integer> unit1 = Unit.fromCollection(list); System.out.println(unit1); } } 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] 5 is present: true [5, Test] 5 [1] Print Page Previous Next Advertisements ”;
Category: javatuples
JavaTuples – Checking Elements ”; Previous Next Each tuple provides utility methods to check their elements in similar fashion as collection. contains(element) − checks if element is present or not. containsAll(collection) − checks if elements are present or not. indexOf(element) − returns the index of first element if present otherwise -1. lastIndexOf(element) − returns the index of last element if present otherwise -1. Pair<String, Integer> pair = Pair.with(“Test”, Integer.valueOf(5)); boolean isPresent = pair.contains(“Test”); Example Let”s see JavaTuples in action. Here we”ll see how to check elements in a tuple. Create a java class file named TupleTester in C:>JavaTuples. File: TupleTester.java package com.tutorialspoint; import java.util.List; import org.javatuples.Quartet; public class TupleTester { public static void main(String args[]){ Quartet<String, Integer, String, String> quartet = Quartet.with( “Test1”, Integer.valueOf(5), “Test3”, “Test3” ); System.out.println(quartet); boolean isPresent = quartet.contains(5); System.out.println(“5 is present: ” + isPresent); isPresent = quartet.containsAll(List.of(“Test1”, “Test3”)); System.out.println(“Test1, Test3 are present: ” + isPresent); int indexOfTest3 = quartet.indexOf(“Test3”); System.out.println(“First Test3 is present at: ” + indexOfTest3); int lastIndexOfTest3 = quartet.lastIndexOf(“Test3”); System.out.println(“Last Test3 is present at: ” + lastIndexOfTest3); } } 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, Test3, Test3] 5 is present: true Test1, Test3 are present: true First Test3 is present at: 2 Last Test3 is present at: 3 Print Page Previous Next Advertisements ”;