JavaTuples fromIterable() method
The fromIterable() method in org.javatuples is used to instance a tuple in a semantically elegant way, with the values of the iterable, given as parameters. This method can be used for any tuple class object of the javatuples library. It is a static function in each javatuple class and it returns the tuple class object of the called class, with the values initialized by the corresponding values of the iterable.
Method Declaration:
public static <X> TupleClass<X> fromIterable(Iterable<X> iterable)
Syntax:
TupleClass<X> obj = TupleClass.fromIterable(Iterable<X> iterable)
Parameters: This method takes iterable as parameter where:
- X– represents the datatype of values in the iterable.
- iterable– represents the iterable of values to be inserted into TupleClass.
- TupleClass– represents the JavaTuple Class used like Unit, Quintet, Decade, etc.
Return Value: This method returns the object of TupleClass, which calls the method, with the values of the iterable, passed as the parameters.
Below programs illustrate the various ways to use fromIterable() method:
Program 1: Using fromIterable() with Unit class:
// Below is a Java program to create // a Unit tuple from fromIterable() method import java.util.*; import org.javatuples.Unit; class GfG { public static void main(String[] args) { // Creating an iterable with one value Iterable<String> itr = Arrays.asList("GeeksforGeeks"); // Using fromIterable() method Unit<String> unit = Unit.fromIterable(itr); System.out.println(unit); } } |
Output:
[GeeksforGeeks]
Program 2: Using fromIterable() with Decade class:
// Below is a Java program to create // a Unit tuple from fromIterable() method import java.util.*; import org.javatuples.Decade; class GfG { public static void main(String[] args) { // Creating an iterable with 10 value Iterable<String> itr = Arrays.asList("GeeksforGeeks", "Geeks", "for", "Geeks", "A", "Computer", "Science", "Portal", "for", "Geeks", "RishabhPrabhu"); // Using fromIterable() method Decade<String, String, String, String, String, String, String, String, String, String> decade = Decade.fromIterable(itr); System.out.println(decade); } } |
Output:
[Geeks, for, Geeks, A, Computer, Science, Portal, for, Geeks, RishabhPrabhu]
Note: Similarly, it can be used with any other JavaTuple Class.
Recommended Posts:
- JavaTuples with() method
- JavaTuples add() method
- JavaTuples contains() method
- JavaTuples compareTo() method
- JavaTuples getLabel() method
- JavaTuples setLabel() method
- JavaTuples setKey() method
- JavaTuples fromCollection() method
- JavaTuples indexOf() method
- JavaTuples addAtX() method
- JavaTuples equal() method
- JavaTuples toString() method
- JavaTuples containsAll() method
- JavaTuples getValue() method
- JavaTuples getSize() method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



