KeyValue Class in JavaTuples

The KeyValue class in the JavaTuples library is a specialized Tuple with two elements: a key and a value. It provides a more meaningful and self-explanatory way to represent key-value pairs than generic Tuples with indices.

As a tuple, the `KeyValue` class possesses all the traits and features inherent to JavaTuples.

  • Immutability: Once created, KeyValue objects cannot be modified. It ensures data consistency and simplifies reasoning about the code.
  • Type safety: KeyValue is generic and can hold any type of value for both key and value, but it enforces type safety by ensuring correct access to each element.
  • Iterable: KeyValue objects can be iterated over, making them convenient for loops and other processing tasks.
  • Serializable: KeyValue objects can be serialized and deserialized, allowing us to store and transmit them between different processes or machines.
  • Comparable: KeyValue objects implement the Comparable interface, enabling us to compare them based on their key-value pairs. It is particularly useful for sorting and searching operations.
  • Equality and hashing: KeyValue objects provide equals() and hashCode() methods for comparing objects based on their content, ensuring consistent behaviour in collections and other data structures.
  • toString(): KeyValue offers a meaningful toString() representation that displays the key and value, making debugging and code readability easier.

Class Declaration

  • IValueKey<A>: The KeyValue class implements the IValueKey interface with the generic type A. It suggests that the class provides implementations for methods related to key operations.
  • IValueValue<B>: Similarly, the class implements the IValueValue interface with the generic type B, indicating that it provides implementations for methods related to value operations.

Class hierarchy

  • Object: The root class for all classes in Java.
  • org.javatuples.Tuple: A class in the javatuples library representing a generic tuple. It serves as the base class for more specific tuple types.
  • org.javatuples.KeyValue<A, B>: A class in the javatuples library representing a key-value pair. It extends the Tuple class and introduces generic types A and B for the key and value. This class is specifically designed for key-value scenarios.

Creating KeyValue Tuple

A `KeyValue` tuple is a specialized tuple in the JavaTuples library designed to represent a key-value pair, providing a convenient way to store and manipulate associated data with distinct key and value components.

Approach: Using Constructor:

When working with JavaTuples, we can use a constructor to create a KeyValue instance with specific key and value values. The KeyValue class typically provides constructors that take the key and value as parameters.

Syntax:

Filename: ConstructorExampleClass.java

Output:

Key 1: Name
Value 1: 25
Key 2: 42
Value 2: 3.14

Approach: Using with() method:

The `with()` method, offered by the JavaTuples library, serves as a function for creating object instances with specific values.

Syntax:

Filename: WithExample.java

Output:

Person 1 - Name: Alice, Age: 28
Person 2 - Name: Bob, Age: 35

From other collections

The `fromCollection()` method is designed to construct a Tuple from a collection, and the `fromArray()` method is used for creating a Tuple from an array. It is essential that both the Collection and the Array share the same data type as the Tuple, and the count of values within the Collection or Array must exactly correspond to the arity of the Tuple class.

Syntax:

Filename: KeyValueExample.java

Output:

KeyValue from List: [Key, Value]
Key: Key
Value: Value

Getting Value

The getValue() and getKey() methods within a KeyValue Tuple are pivotal for accessing the encapsulated value and key, respectively. These methods provide a straightforward means to retrieve the associated value and key from a KeyValue Tuple instance.

getKey():

The getKey() method in the org.javatuples library is designed for use with the KeyValue class. It retrieves the key from a KeyValue object and ensures type safety. Specifically, it returns the element at index 0 within the KeyValue object. This method is exclusive to KeyValue objects in the javatuples library.

Syntax:

Filename: GetKeyExampleClass.java

Output:

Value: 42

getValue():

The `getValue()` method in the `org.javatuples` library retrieves the value at a specified index within a Tuple object. It returns an `Object` value, potentially compromising type-safety, as the data type of the returned value is not explicitly specified.

Syntax:

Filename: GetValueExampleClass.java

Output:

Key: exampleKey
Value: 42

Setting KeyValue Value

In Java programming, immutability ensures that its elements cannot be modified directly once a tuple is created. JavaTuples provides methods like setKey() and setValue() to create new instances with updated values, maintaining the immutability of the original KeyValue object.

setKey()

The setKey() method in JavaTuples allows the creation of a new KeyValue instance with an updated key while keeping the original Tuple immutable. The method ensures type safety and consistency and aligns with functional programming principles by returning a modified copy of the KeyValue object.

Syntax:

Filename: SetKeyExample.java

Output:

Original KeyValue: 1 - OriginalValue
Modified KeyValue: 2 - OriginalValue

setValue():

The setValue() method in JavaTuples allows us to create a new KeyValue instance with an updated value while maintaining the immutability of the original Tuple. The method is useful when working with different values associated with the same key.

Syntax:

Filename: SetValueExample.java

Output:

Original KeyValue: 1 - OriginalValue
Modified KeyValue: 1 - NewValue

Searching in KeyValue

Searching for specific elements within a key-value pair is a common operation in programming, often used to determine the presence or absence of a particular value associated with a given key. In the context of tuples, such as those found in the JavaTuples library, a predefined method called contains() provides a convenient way to perform these searches. This method returns a boolean value, indicating whether the specified value is present in the Tuple.

Syntax:

Filename: KeyValueSearchExample.java

Output:

Exists: true
Exists1: false
KeyValue 1 is equal to KeyValue 2: false

Iterating through KeyValue

Iterating through key-value pairs is a fundamental process in programming, essential for working with data structures that associate values with unique keys. It involves accessing and processing each pair within a data structure, enabling tasks like searching, updating, and performing computations based on the stored information.

Syntax:

Filename: KeyValueIterationExample.java

Output:

Key: 1, Value: JavaTpoint
Key: 2, Value: JavaTuples
Key: 3, Value: Programming





Latest Courses