A Quartet is a Tuple from JavaTuples library that deals with 3 elements. Since this Quartet is a generic class, it can hold any type of value in it.
Since Quartet is a Tuple, hence it also has all the characterstics of JavaTuples:
- They are Typesafe
- They are Immutable
- They are Iterable
- They are Serializable
- They are Comparable (implements Comparable<Tuple>)
- They implement equals() and hashCode()
- They also implement toString()
Class Declaration
public final class Quartet<A, B, C, D> extends Tuple implements IValue0<A>, IValue1<B>, IValue2<C>, IValue3<D>
Class hierarchy
Object ↳ org.javatuples.Tuple ↳ org.javatuples.Quartet<A, B, C, D>
Creating Quartet Tuple
- From Constructor:
Syntax:
Quartet<A, B, C, D> quartet = new Quartet<A, B, C, D> (value1, value2, value3, value4);
Example:
// Below is a Java program to create
// a Quartet tuple from Constructor
import
java.util.*;
import
org.javatuples.Quartet;
class
GfG {
public
static
void
main(String[] args)
{
Quartet<Integer, String, String, Double> quartet
= Quartet.with(Integer.valueOf(
1
),
"GeeksforGeeks"
,
"A computer portal"
,
Double.valueOf(
20.18
));
System.out.println(quartet);
}
}
Output:
br>
[1, GeeksforGeeks, A computer portal, 20.18]
- Using with() method: The with() method is a function provided by the JavaTuples library, to instantiate the object with such values.
Syntax:
Quartet<type1, type2, type3, type4> quartet = Quartet.with(value1, value2, value3, value4);
Example:
// Below is a Java program to create
// a Quartet tuple from with() method
import
java.util.*;
import
org.javatuples.Quartet;
class
GfG {
public
static
void
main(String[] args)
{
Quartet<Integer, String, String, Double> quartet
= Quartet.with(Integer.valueOf(
1
),
"GeeksforGeeks"
,
"A computer portal"
,
Double.valueOf(
20.18
));
System.out.println(quartet);
}
}
Output:
[1, GeeksforGeeks, A computer portal, 20.18]
- From other collections: The fromCollection() method is used to create a Tuple from a collection, and fromArray() method is used to create from an array. The collection/array must have the same type as of the Tuple and the number of values in the collection/array must match the Tuple class.
Syntax:
Quartet<type1, type2, type3, type4> quartet = Quartet.fromCollection(collectionWith_2_value); Quartet<type1, type2, type3, type4> quartet = Quartet.fromArray(arrayWith_2_value);
Example:
// Below is a Java program to create
// a Quartet tuple from Collection
import
java.util.*;
import
org.javatuples.Quartet;
class
GfG {
public
static
void
main(String[] args)
{
// Creating Quartet from List
List<String> list =
new
ArrayList<String>();
list.add(
"GeeksforGeeks"
);
list.add(
"A computer portal"
);
list.add(
"for geeks"
);
list.add(
"by Sandeep Jain"
);
Quartet<Strin, String, String, String> quartet
= Quartet.fromCollection(list);
// Creating Quartet from Array
String[] arr = {
"GeeksforGeeks"
,
"A computer portal"
,
"for geeks"
,
"by Sandeep Jain"
};
Quartet<String, String, String, String> otherQuartet
= Quartet.fromArray(arr);
System.out.println(quartet);
System.out.println(otherQuartet);
}
}
Output:
[GeeksforGeeks, A computer portal, for geeks, by Sandeep Jain] [GeeksforGeeks, A computer portal, for geeks, by Sandeep Jain]
Getting Value
The getValueX() method can be used to fetch the value in a Tuple at index X. The indexing in Tuples starts with 0. Hence the value at index X represents the value at position X+1.
Syntax:
Quartet<type1, type2, type3, type4> quartet = new Quartet<type1, type2, type3, type4>(value1, value2, value3, value4); type1 val1 = quartet.getValue0();
Example:
// Below is a Java program to get // a Quartet value import java.util.*; import org.javatuples.Quartet; class GfG { public static void main(String[] args) { Quartet<Integer, String, String, Double> quartet = Quartet.with(Integer.valueOf( 1 ), "GeeksforGeeks" , "A computer portal" , Double.valueOf( 20.18 )); System.out.println(quartet.getValue0()); System.out.println(quartet.getValue2()); } } |
Output:
1 A computer portal
Setting Quartet Value
Since the Tuples are immutable, it means that modifying a value at any index is not possible. Hence, JavaTuples offer setAtX(value) which creates a copy of the Tuple with a new value at index X, and returns that Tuple.
Syntax:
Quartet<type1, type2, type3, type4> quartet = new Quartet<type1, type2, type3, type4> (value1, value2, value3, value4); Quartet<type1, type2, type3, type4> otherQuartet = quartet.setAtX(value);
Example:
// Below is a Java program to set // a Quartet value import java.util.*; import org.javatuples.Quartet; class GfG { public static void main(String[] args) { Quartet<Integer, String, String, Double> quartet = Quartet.with(Integer.valueOf( 1 ), "GeeksforGeeks" , "A computer portal" , Double.valueOf( 20.18 )); Quartet<Integer, String, String, Double> otherQuartet = quartet.setAt3( 2.018 ); System.out.println(otherQuartet); } } |
Output:
[1, GeeksforGeeks, A computer portal, 2.018]
Adding a Value
Adding a value can be done with the help of addAtX() method, where X represent the index at which the value is to be added. This method returns a Tuple of element one more than the called Tuple.
Syntax:
Quartet<type1, type2, type3, type4> quartet = new Quartet<type1, type2, type3, type4> (value1, value2, value3, value4); Quintet<type 1, type 2, type 3, type 4, type 5> quintet = quartet.addAtx(value);
Example:
// Below is a Java program to add // a value import java.util.*; import org.javatuples.Quartet; import org.javatuples.Quintet; class GfG { public static void main(String[] args) { Quartet<Integer, String, String, Double> quartet = Quartet.with(Integer.valueOf( 1 ), "GeeksforGeeks" , "A computer portal" , Double.valueOf( 20.18 )); Quintet<Integer, String, String, Double, Boolean> quintet = quartet.addAt4( true ); System.out.println(quintet); } } |
Output:
[1, GeeksforGeeks, A computer portal, for geeks, 20.18, true]
Searching in Quartet
An element can be searched in a tuple with the pre-defined method contains(). It returns a boolean value whether the value is present or not.
Syntax:
Quartet<type1, type2, type3, type4> quartet = new Quartet<type1, type2, type3, type4>(value1, value2, value3, value4); boolean res = quartet.contains(value2);
Example:
// Below is a Java program to search // a value in a Quartet import java.util.*; import org.javatuples.Quartet; class GfG { public static void main(String[] args) { Quartet<Integer, String, String, Double> quartet = Quartet.with(Integer.valueOf( 1 ), "GeeksforGeeks" , "A computer portal" , Double.valueOf( 20.18 )); boolean exist = quartet.contains( 20.18 ); boolean exist1 = quartet.contains( 4 ); System.out.println(exist); System.out.println(exist1); } } |
Output:
true false
Iterating through Quartet
Since Quartet implement the Iterable<Object> interface. It means that they can be iterated in the same way as collections or arrays.
Syntax:
Quartet<type1, type2, type3, type4> quartet = new Quartet<type1, type2, type3, type4> (value1, value2, value3, value4); for (Object item : quartet) { ... }
Example:
// Below is a Java program to iterate // a Quartet import java.util.*; import org.javatuples.Quartet; class GfG { public static void main(String[] args) { Quartet<Integer, String, String, Double> quartet = Quartet.with(Integer.valueOf( 1 ), "GeeksforGeeks" , "A computer portal" , Double.valueOf( 20.18 )); for (Object item : quartet) System.out.println(item); } } |
Output:
1 GeeksforGeeks A computer portal 20.18
leave a comment
0 Comments