Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JavaTuples setValue() method

  • Last Updated : 27 Aug, 2018

The setValue() method in org.javatuples is used to set the value in the LabelValueClassObject or KeyValueClassObject. This method can be used with only LabelValue class or KeyValue class of javatuples library. It returns another ClassObject with the value as the element passed as the parameter, and the key or label from the previous ClassObject.

    LabelValue.setValue()

    Method Declaration:

    Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

    public <X> LabelValue<A, X> setValue(X value)

    Syntax:

    LabelValue<A, B> LabelValueClassObject = LabelValue.setValue(X value)

    Return Value: This method returns another LabelValueClassObject with the value as the element passed as the parameter, and the label from the previous LabelValueClassObject.



    Below is the program that will illustrate the various ways to use setValue() method in LabelValue Class:

    Example:




    // Below is a Java program to set
    // Value in a LabelValue pair
      
    import java.util.*;
    import org.javatuples.LabelValue;
      
    class GfG {
        public static void main(String[] args)
        {
            // Creating a LabelValue object
            LabelValue<Integer, String> lv
                = LabelValue.with(Integer.valueOf(1),
                                  "A computer science portal");
      
            // Using setValue() method
            LabelValue<Integer, Integer> lv1 = lv.setValue(Integer.valueOf(2));
      
            // Printing the returned LabelValue
            System.out.println(lv1);
        }
    }

    Output:

    [1, 2]
    

    KeyValue.setValue()

    Method Declaration:

    public <X> KeyValue<A, X> setValue(X value)

    Syntax:

    KeyValue<A, B> KeyValueClassObject = KeyValue.setValue(X value)

    Return Value: This method returns another KeyValueClassObject with the value as the element passed as the parameter, and the key from the previous KeyValueClassObject.

    Below is the program that will illustrate the various ways to use setValue() method in KeyValue Class:

    Example:




    // Below is a Java program to set
    // Value in a KeyValue pair
      
    import java.util.*;
    import org.javatuples.KeyValue;
      
    class GfG {
        public static void main(String[] args)
        {
            // Creating a KeyValue object
            KeyValue<Integer, String> kv
                = KeyValue.with(Integer.valueOf(1),
                                "A computer science portal");
      
            // Using setValue() method
            KeyValue<Integer, String> kv1 = kv.setValue("GeeksforGeeks");
      
            // Printing the returned KeyValue
            System.out.println(kv1);
        }
    }

    Output:

    [1, GeeksforGeeks]
    



My Personal Notes arrow_drop_up
Recommended Articles
Page :