![]() |
VOOZH | about |
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:
public final class LabelValue<A, B> extends Tuple implements IValueLabel<A>, IValueValue<B>
Object ↳ org.javatuples.Tuple ↳ org.javatuples.LabelValue<A, B>
LabelValue<A, B> kv = new LabelValue<A, B>(value1, value2);
Output:
[1, GeeksforGeeks]
LabelValue<type1, type2> kv = LabelValue.with(value1, value2);
Output:
[1, GeeksforGeeks]
LabelValue<type1, type2> kv = LabelValue.fromCollection(collectionWith_2_value); LabelValue<type1, type2> kv = LabelValue.fromArray(arrayWith_2_value);
Output:
[GeeksforGeeks, A computer portal] [GeeksforGeeks, A computer portal]
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();
Output:
1
LabelValue<type1, type2> kv = new LabelValue<type1, type2>(value1, value2); type2 val1 = kv.getValue();
Output:
GeeksforGeeks
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);
Output:
[10, GeeksforGeeks]
LabelValue<type1, type2> kv = new LabelValue<type1, type2>(value1, value2); LabelValue<type1, type2> kvNew = kv.setValue(valueNew);
Output:
[1, A computer science portal]
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:
Output:
true false
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:
Output:
1 GeeksforGeeks