Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create Quartet Tuple in Java
Java does not have built-in tuple support; to use the tuple, we use the third-party library called Javatuples. This library provides classes representing fixed-size tuples of different sizes. The Quartet class is one of these classes that represents a tuple with 4 values.
Creating a Quartet Tuple in Java
We can create a Quartet in Java in the following ways:
Using a Constructor
The constructor of the Quartet class accepts 4 values and creates a Quartet object.
Quartet(A value0, B value1, C value2, D value3)
We can create a Quartet tuple by passing four values to its constructor as shown below:
Quartet<String, Integer, String, Double> q = new Quartet<>("Book", 10, "Code", 99.99);
Example
Following example creates a Quartet object using its constructor:
import org.javatuples.Quartet;
public class Demo {
public static void main(String[] args) {
Quartet<String, String, String, String> q = new Quartet<>("A", "B", "C", "D");
System.out.println(q);
}
}
Output of the above code is:
[A, B, C, D]
Using fromCollection() Method
We can create a Quartet object by passing a collection to the fromCollection() method (of the same class). The given collection object should contain exactly 4 elements; otherwise, this method throws an IllegalArgumentException.
Example
In the following example, we are creating a Quartet object from a Set:
import java.util.*;
import org.javatuples.Quartet;
public class Demo {
public static void main(String[] args) {
Set<String> mySet = new LinkedHashSet<>();
mySet.add("One");
mySet.add("Two");
mySet.add("Three");
mySet.add("Four");
Quartet<String, String, String, String> q = Quartet.fromCollection(mySet);
System.out.println(q);
}
}
Output of the above code is:
[One, Two, Three, Four]
Example
In the following example, we are creating a Quartet object from a List:
import java.util.*;
import org.javatuples.Quartet;
public class Demo {
public static void main(String[] args) {
List<String> myList = new ArrayList<>();
myList.add("Spring");
myList.add("Summer");
myList.add("Autumn");
myList.add("Winter");
Quartet<String, String, String, String> q = Quartet.fromCollection(myList);
System.out.println(q);
}
}
Output of the above code is:
[Spring, Summer, Autumn, Winter]
Using with() method
The with() method is a static method accepts four values and creates a Quartet object (Similar to a collection). Following is the Syntax:
Quartet.with("A", "B", "C", "D");
Example
In this example, we are creating a Quartet using the with() method:
import org.javatuples.Quartet;
public class Demo {
public static void main(String[] args) {
Quartet<String, String, String, String> q = Quartet.with("Red", "Green", "Blue", "Yellow");
System.out.println(q);
}
}
Output of the above code is:
[Red, Green, Blue, Yellow]
Using fromArray() method
We can also create a Quartet object by passing an array (with four elements) to the fromArray() method. Following is the syntax:
fromArray(colors);
Example
In this example, we are creating a Quartet using the fromArray() method:
import org.javatuples.Quartet;
public class Demo {
public static void main(String[] args) {
String[] colors = {"Pink", "Purple", "Cyan", "Orange"};
Quartet<String, String, String, String> q = Quartet.fromArray(colors);
System.out.println(q);
}
}
Output of the above code is:
[Pink, Purple, Cyan, Orange]