LabelValue Class in JavaTuples
Last Updated :
03 Aug, 2021
A LabelValue is a Tuple from JavaTuples library that deals with only 2 elements - a label and a value. Since this LabelValue is a generic class, it can hold any type of value in it.
Since LabelValue is a Tuple, hence it also has all the characteristics of JavaTuples:
- They are Typesafe
- They are Immutable
- They are Iterable
- They are Serializable
- They are Comparable (implements Comparable<Tuple>)
- They implement equals() and hashCode()
- They also implement toString()
Class Declaration
public final class LabelValue<A, B> extends Tuple
implements IValueLabel<A>, IValueValue<B>
Class hierarchy
Object
↳ org.javatuples.Tuple
↳ org.javatuples.LabelValue<A, B>
Creating LabelValue Tuple
LabelValue<A, B> kv = new LabelValue<A, B>(value1, value2);
Java
// Below is a Java program to create
// a LabelValue tuple from Constructor
import java.util.*;
import org.javatuples.LabelValue;
class GfG {
public static void main(String[] args)
{
LabelValue<Integer, String> kv
= new LabelValue<Integer, String>(Integer.valueOf(1),
"GeeksforGeeks");
System.out.println(kv);
}
}
Output:
[1, GeeksforGeeks]
- Using with() method: The with() method is a function provided by the JavaTuples library, to instantiate the object with such values.
Syntax:
LabelValue<type1, type2> kv = LabelValue.with(value1, value2);
Java
// Below is a Java program to create
// a LabelValue tuple from with() method
import java.util.*;
import org.javatuples.LabelValue;
class GfG {
public static void main(String[] args)
{
LabelValue<Integer, String> kv
= LabelValue.with(Integer.valueOf(1), "GeeksforGeeks");
System.out.println(kv);
}
}
Output:
[1, GeeksforGeeks]
- From other collections: The fromCollection() method is used to create a Tuple from a collection, and fromArray() method is used to create from an array. The collection/array must have the same type as of the Tuple and the number of values in the collection/array must match the Tuple class.
Syntax:
LabelValue<type1, type2> kv = LabelValue.fromCollection(collectionWith_2_value);
LabelValue<type1, type2> kv = LabelValue.fromArray(arrayWith_2_value);
Java
// Below is a Java program to create
// a LabelValue tuple from Collection
import java.util.*;
import org.javatuples.LabelValue;
class GfG {
public static void main(String[] args)
{
// Creating LabelValue from List
List<String> list = new ArrayList<String>();
list.add("GeeksforGeeks");
list.add("A computer portal");
LabelValue<String, String> kv
= LabelValue.fromCollection(list);
// Creating LabelValue from Array
String[] arr = { "GeeksforGeeks", "A computer portal" };
LabelValue<String, String> otherLabelValue
= LabelValue.fromArray(arr);
System.out.println(kv);
System.out.println(otherLabelValue);
}
}
Output:
[GeeksforGeeks, A computer portal]
[GeeksforGeeks, A computer portal]
Getting Value
The getValue() and getLabel() methods can be used to fetch the value and key respectively in a LabelValue Tuple.
LabelValue<type1, type2> kv =
new LabelValue<type1, type2>(value1, value2);
type2 val1 = kv.getLabel();
Java
// Below is a Java program to get
// a LabelValue value
import java.util.*;
import org.javatuples.LabelValue;
class GfG {
public static void main(String[] args)
{
LabelValue<Integer, String> kv
= LabelValue.with(Integer.valueOf(1), "GeeksforGeeks");
System.out.println(kv.getLabel());
}
}
Output:
1
LabelValue<type1, type2> kv =
new LabelValue<type1, type2>(value1, value2);
type2 val1 = kv.getValue();
Java
// Below is a Java program to get
// a LabelValue value
import java.util.*;
import org.javatuples.LabelValue;
class GfG {
public static void main(String[] args)
{
LabelValue<Integer, String> kv
= LabelValue.with(Integer.valueOf(1), "GeeksforGeeks");
System.out.println(kv.getValue());
}
}
Output:
GeeksforGeeks
Setting LabelValue Value
Since the Tuples are immutable, it means that modifying a value at an index is not possible. Hence, JavaTuples offer setLabel(value) and setValue(value) which creates a copy of the LabelValue with a new value according to method used, and returns a new LabelValue object.
LabelValue<type1, type2> kv =
new LabelValue<type1, type2>(value1, value2);
LabelValue<type1, type2> kvNew = kv.setLabel(valueNew);
Java
// Below is a Java program to set
// a LabelValue Key
import java.util.*;
import org.javatuples.LabelValue;
class GfG {
public static void main(String[] args)
{
LabelValue<Integer, String> kv
= LabelValue.with(Integer.valueOf(1), "GeeksforGeeks");
LabelValue<Integer, String> otherLabelValue
= kv.setLabel(10);
System.out.println(otherLabelValue);
}
}
Output:
[10, GeeksforGeeks]
LabelValue<type1, type2> kv =
new LabelValue<type1, type2>(value1, value2);
LabelValue<type1, type2> kvNew = kv.setValue(valueNew);
Java
// Below is a Java program to set
// a LabelValue Value
import java.util.*;
import org.javatuples.LabelValue;
class GfG {
public static void main(String[] args)
{
LabelValue<Integer, String> kv
= LabelValue.with(Integer.valueOf(1), "GeeksforGeeks");
LabelValue<Integer, String> otherLabelValue
= kv.setValue("A computer science portal");
System.out.println(otherLabelValue);
}
}
Output:
[1, A computer science portal]
Searching in LabelValue
An element can be searched in a tuple with the pre-defined method contains(). It returns a boolean value whether the value is present or not.
Syntax:
LabelValue<type1, type2> kv =
new LabelValue<type1, type2>(value1, value2);
boolean res = kv.contains(value2);
Example:
Java
// Below is a Java program to search
// a value
import java.util.*;
import org.javatuples.LabelValue;
class GfG {
public static void main(String[] args)
{
LabelValue<Integer, String> kv
= LabelValue.with(Integer.valueOf(1), "GeeksforGeeks");
// Using contains for True result
boolean exist = kv.contains("GeeksforGeeks");
// Using contains for False result
boolean exist1 = kv.contains(4);
System.out.println(exist);
System.out.println(exist1);
}
}
Output:
true
false
Iterating through LabelValue
Since LabelValue implement the Iterable<Object> interface. It means that they can be iterated in the same way as collections or arrays.
Syntax:
LabelValue<type1, type2> kv =
new LabelValue<type1, type2>(value1, value2);
for (Object item : kv) {
...
}
Example:
Java
// Below is a Java program to iterate
// a LabelValue
import java.util.*;
import org.javatuples.LabelValue;
class GfG {
public static void main(String[] args)
{
LabelValue<Integer, String> kv
= LabelValue.with(Integer.valueOf(1), "GeeksforGeeks");
for (Object item : kv)
System.out.println(item);
}
}
Output:
1
GeeksforGeeks
Similar Reads
JavaTuples | Introduction
Tuple The word "tuple" means "a data structure consisting of multiple parts". Hence Tuples can be defined as a data structure that can hold multiple values and these values may/may not be related to each other. Example: [Geeks, 123, *@] In this example, the values in the tuple are not at all relat
6 min read
Unit Class in JavaTuples
The Unit class in JavaTuples is a tuple with no values. It is often used as a placeholder when you need to represent an empty tuple, for example, as the second value in a Pair when you only need the first value. The Unit class is a subclass of the Tuple class, which means that it has all the methods
7 min read
Pair Class in JavaTuples
A Pair is a Tuple from JavaTuples library that deals with 2 elements. Since this Pair is a generic class, it can hold any type of value in it.Since Pair is a Tuple, hence it also has all the characteristics of JavaTuples:Â They are TypesafeThey are ImmutableThey are IterableThey are SerializableThey
5 min read
Triplet Class in JavaTuples
A Triplet is a Tuple from JavaTuples library that deals with 3 elements. Since this Triplet is a generic class, it can hold any type of value in it. Since Triplet is a Tuple, hence it also has all the characteristics of JavaTuples:Â They are TypesafeThey are ImmutableThey are IterableThey are Serial
5 min read
Quartet Class in JavaTuples
A Quartet is a Tuple from JavaTuples library that deals with 4 elements. Since this Quartet is a generic class, it can hold any type of value in it. Since Quartet is a Tuple, hence it also has all the characteristics of JavaTuples:Â They are TypesafeThey are ImmutableThey are IterableThey are Serial
5 min read
Quintet Class in JavaTuples
A Quintet is a Tuple from JavaTuples library that deals with 3 elements. Since this Quintet is a generic class, it can hold any type of value in it.Since Quintet is a Tuple, hence it also has all the characteristics of JavaTuples:Â Â They are TypesafeThey are ImmutableThey are IterableThey are Serial
5 min read
Sextet Class in JavaTuples
A Sextet is a Tuple from JavaTuples library that deals with 3 elements. Since this Sextet is a generic class, it can hold any type of value in it.Since Sextet is a Tuple, hence it also has all the characteristics of JavaTuples:Â Â They are TypesafeThey are ImmutableThey are IterableThey are Serializa
6 min read
Septet Class in JavaTuples
A Septet is a Tuple from JavaTuples library that deals with 3 elements. Since this Septet is a generic class, it can hold any type of value in it.Since Septet is a Tuple, hence it also has all the characteristics of JavaTuples:Â Â They are TypesafeThey are ImmutableThey are IterableThey are Serializa
6 min read
Octet Class in JavaTuples
A Octet is a Tuple from JavaTuples library that deals with 3 elements. Since this Octet is a generic class, it can hold any type of value in it.Since Octet is a Tuple, hence it also has all the characteristics of JavaTuples:Â Â They are TypesafeThey are ImmutableThey are IterableThey are Serializable
6 min read
Ennead Class in JavaTuples
A Ennead is a Tuple from JavaTuples library that deals with 9 elements. Since this Ennead is a generic class, it can hold any type of value in it. Since Ennead is a Tuple, hence it also has all the characteristics of JavaTuples:Â They are TypesafeThey are ImmutableThey are IterableThey are Serializa
6 min read