LabelValue Class in JavaTuples
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 characterstics 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
- From Constructor:
Syntax:
LabelValue<A, B> kv = new LabelValue<A, B>(value1, value2);
Example:
// Below is a Java program to create// a LabelValue tuple from Constructorimportjava.util.*;importorg.javatuples.LabelValue;classGfG {publicstaticvoidmain(String[] args){LabelValue<Integer, String> kv=newLabelValue<Integer, String>(Integer.valueOf(1),"GeeksforGeeks");System.out.println(kv);}}chevron_rightfilter_noneOutput:
[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);
Example:
// Below is a Java program to create// a LabelValue tuple from with() methodimportjava.util.*;importorg.javatuples.LabelValue;classGfG {publicstaticvoidmain(String[] args){LabelValue<Integer, String> kv= LabelValue.with(Integer.valueOf(1),"GeeksforGeeks");System.out.println(kv);}}chevron_rightfilter_noneOutput:
[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);
Example:
// Below is a Java program to create// a LabelValue tuple from Collectionimportjava.util.*;importorg.javatuples.LabelValue;classGfG {publicstaticvoidmain(String[] args){// Creating LabelValue from ListList<String> list =newArrayList<String>();list.add("GeeksforGeeks");list.add("A computer portal");LabelValue<String, String> kv= LabelValue.fromCollection(list);// Creating LabelValue from ArrayString[] arr = {"GeeksforGeeks","A computer portal"};LabelValue<String, String> otherLabelValue= LabelValue.fromArray(arr);System.out.println(kv);System.out.println(otherLabelValue);}}chevron_rightfilter_noneOutput:
[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.
- getLabel():
Syntax:
LabelValue<type1, type2> kv = new LabelValue<type1, type2>(value1, value2); type2 val1 = kv.getLabel();Example:
// Below is a Java program to get// a LabelValue valueimportjava.util.*;importorg.javatuples.LabelValue;classGfG {publicstaticvoidmain(String[] args){LabelValue<Integer, String> kv= LabelValue.with(Integer.valueOf(1),"GeeksforGeeks");System.out.println(kv.getLabel());}}chevron_rightfilter_noneOutput:
1
- getValue():
Syntax:
LabelValue<type1, type2> kv = new LabelValue<type1, type2>(value1, value2); type2 val1 = kv.getValue();Example:
// Below is a Java program to get// a LabelValue valueimportjava.util.*;importorg.javatuples.LabelValue;classGfG {publicstaticvoidmain(String[] args){LabelValue<Integer, String> kv= LabelValue.with(Integer.valueOf(1),"GeeksforGeeks");System.out.println(kv.getValue());}}chevron_rightfilter_noneOutput:
GeeksforGeeks
- setLabel():
Syntax:
LabelValue<type1, type2> kv = new LabelValue<type1, type2>(value1, value2); LabelValue<type1, type2> kvNew = kv.setLabel(valueNew);Example:
// Below is a Java program to set// a LabelValue Keyimportjava.util.*;importorg.javatuples.LabelValue;classGfG {publicstaticvoidmain(String[] args){LabelValue<Integer, String> kv= LabelValue.with(Integer.valueOf(1),"GeeksforGeeks");LabelValue<Integer, String> otherLabelValue= kv.setLabel(10);System.out.println(otherLabelValue);}}chevron_rightfilter_noneOutput:
[10, GeeksforGeeks]
- setValue():
Syntax:
LabelValue<type1, type2> kv = new LabelValue<type1, type2>(value1, value2); LabelValue<type1, type2> kvNew = kv.setValue(valueNew);Example:
// Below is a Java program to set// a LabelValue Valueimportjava.util.*;importorg.javatuples.LabelValue;classGfG {publicstaticvoidmain(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);}}chevron_rightfilter_noneOutput:
[1, A computer science portal]
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.
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:
// 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:
// 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
Recommended Posts:
- Implement Quartet Class with Triplet Class in Java using JavaTuples
- Implement Quintet Class with Quartet Class in Java using JavaTuples
- Implement Triplet Class with Pair Class in Java using JavaTuples
- Implement Pair Class with Unit Class in Java using JavaTuples
- Implement Octet Class from Septet Class in Java using JavaTuples
- Implement Decade Class from Ennead Class in Java using JavaTuples
- Implement Ennead Class from Octet Class in Java using JavaTuples
- Implement Sextet Class from Quintet Class in Java using JavaTuples
- Implement Septet Class from Sextet Class in Java using JavaTuples
- Octet Class in JavaTuples
- Decade Class in JavaTuples
- Unit Class in JavaTuples
- Ennead Class in JavaTuples
- KeyValue Class in JavaTuples
- Quintet Class in JavaTuples
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.



