Open In App

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.

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]

Article Tags :