Thursday, June 11, 2026
HomeLanguagesJavaJavaTuples fromCollection() method

JavaTuples fromCollection() method

The fromCollection() method in org.javatuples is used to instantiate a tuple in a semantically elegant way, with the values of the collection, given as parameters. This method can be used to any tuple class object of 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 collection.

Method Declaration:

public static <X> TupleClass<X> fromCollection(Collection<X> collection)

Syntax:

TupleClass<X> obj = TupleClass.fromCollection(Collection<X> collection)

Parameters: This method takes collection as parameter where:

  • X– represents the datatype of values in the collection.
  • collection– represents the collection 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 collection, passed as the parameters.

Below programs illustrate the various ways to use fromCollection() method:

Program 1: Using fromCollection() with Unit class:




// Below is a Java program to create
// a Unit tuple from fromCollection() method
  
import java.util.*;
import org.javatuples.Unit;
  
class GfG {
    public static void main(String[] args)
    {
        // Creating an collection with one value
        List<String> list = new ArrayList<String>();
        list.add("Lazyroar");
  
        // Using fromCollection() method
        Unit<String> unit = Unit.fromCollection(list);
  
        System.out.println(unit);
    }
}


Output:

[Lazyroar]

Program 2: Using fromCollection() with Decade class:




// Below is a Java program to create
// a Unit tuple from fromCollection() method
  
import java.util.*;
import org.javatuples.Decade;
  
class GfG {
    public static void main(String[] args)
    {
        // Creating an collection with 10 value
        List<String> list = new ArrayList<String>();
        list.add("Geeks");
        list.add("for");
        list.add("Geeks");
        list.add("A");
        list.add("Computer");
        list.add("Science");
        list.add("Portal");
        list.add("for");
        list.add("Geeks");
        list.add("RishabhPrabhu");
  
        // Using fromCollection() method
        Decade<String, String, String, String, String,
               String, String, String, String, String>
            decade = Decade.fromCollection(list);
  
        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.

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6896 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS