KeyValue Class in JavaTuplesThe 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.
Class Declaration
Class hierarchy
Creating KeyValue TupleA `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 collectionsThe `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 ValueThe 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 ValueIn 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 KeyValueSearching 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 KeyValueIterating 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 Next TopicQuantifiers in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India