JavaTuples setValue() method

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:

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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);
        }
    }

    chevron_right

    
    

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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);
        }
    }

    chevron_right

    
    

    Output:

    [1, GeeksforGeeks]
    


My Personal Notes arrow_drop_up

Technical Content Engineer at GeeksForGeeks

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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.