
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 Septet 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 Septet class is one of these classes that represents a tuple with 7 values.
Creating a Septet Tuple in Java
We can create a Septet in Java in the following ways:
Using a Constructor
The constructor of the Septet class accepts 7 values and creates a Septet object.
Septet(A value0, B value1, C value2, D value3, E value4, F value5, G value6)
We can create a Septet tuple by passing seven values to its constructor as shown below:
Septet<String, String, String, String, String, String, String> s = new Septet<>("A", "B", "C", "D", "E", "F", "G");
Example
Following example creates a Septet object using its constructor:
import org.javatuples.Septet; public class Demo { public static void main(String[] args) { Septet<String, String, String, String, String, String, String> s = new Septet<>("A", "B", "C", "D", "E", "F", "G"); System.out.println(s); } }
Output of the above code is:
[A, B, C, D, E, F, G]
Using fromCollection() Method
We can create a Septet object by passing a collection to the fromCollection() method (of the same class). The given collection object should contain exactly 7 elements; otherwise, this method throws an IllegalArgumentException.
Example
In the following example, we are creating a Septet object from a Set:
import java.util.*; import org.javatuples.Septet; public class Demo { public static void main(String[] args) { Set<String> set = new LinkedHashSet<>(); set.add("One"); set.add("Two"); set.add("Three"); set.add("Four"); set.add("Five"); set.add("Six"); set.add("Seven"); Septet<String, String, String, String, String, String, String> s = Septet.fromCollection(set); System.out.println(s); } }
Output of the above code is:
[One, Two, Three, Four, Five, Six, Seven]
Example
In the following example, we are creating a Septet object from a List:
import java.util.*; import org.javatuples.Septet; public class Demo { public static void main(String[] args) { List<String> list = Arrays.asList("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"); Septet<String, String, String, String, String, String, String> s = Septet.fromCollection(list); System.out.println(s); } }
Output of the above code is:
[Sun, Mon, Tue, Wed, Thu, Fri, Sat]
Using with() method
The with() method is a static method that accepts seven values and creates a Septet object. Following is the Syntax:
Septet.with("A", "B", "C", "D", "E", "F", "G");
Example
In this example, we are creating a Septet using the with() method:
import org.javatuples.Septet; public class Demo { public static void main(String[] args) { Septet<String, String, String, String, String, String, String> s = Septet.with("Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet"); System.out.println(s); } }
Output of the above code is:
[Red, Orange, Yellow, Green, Blue, Indigo, Violet]
Using fromArray() method
We can also create a Septet object by passing an array (with seven elements) to the fromArray() method. Following is the syntax:
fromArray(values);
Example
In this example, we are creating a Septet using the fromArray() method:
import org.javatuples.Septet; public class Demo { public static void main(String[] args) { String[] planets = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus"}; Septet<String, String, String, String, String, String, String> s = Septet.fromArray(planets); System.out.println(s); } }
Output of the above code is:
[Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus]
Conclusion
The Septet class in the Javatuples library is used to store and handle exactly seven values in a single object. The class offers multiple methods for instantiation, including with(), fromCollection(), and fromArray().