| by Arround The Web | No comments

How to Create a Pair Class in Java

In Java, there can be certain instances where the developer needs to allocate custom “key-value” pairs from time to time. For instance, writing a template code to accumulate the values comprising multiple data types. In such cases, creating a “pair class” in Java assists the programmer in associating and utilizing the key-value pairs effectively.

This blog will elaborate on creating a “pair class” in Java.

What is a “Pair Class” in Java?

A “pair class” provides an effective way of associating the “key-value” pairs. This approach is helpful when two values need to be returned from a method. For instance, computing the square root of a number and returning both the square root and the number itself, i.e., “(9,3)” etc.

How to Create a “Pair Class” in Java?

A pair class in Java can be created by setting the key-value pair via the class object and retrieving it with the help of the getter method.

Example 1: Creating a Pair Class of “Integer” Type in Java
In this example, a pair class of “Integer” data type can be created such that the key-value pair is passed in the form of integers:

class Template<T> {
 T val1, val2;
 void setValue(T x, T y){
  this.val1 = x;
  this.val2 = y;
}
 Template getValue(){
  return this;
}}
class pairclass{
 public static void main(String args[]){
  Template<Integer> object = new Template<Integer>();
  System.out.println("The key-value pair is: ");
  object.setValue(5,10);
  Template <Integer> result= new Template <Integer> ();
  result = object.getValue();
  System.out.println(result.val1 + " " + result.val2);
}}

In the above lines of code, apply the following steps:

  • Firstly, declare a class named “Template”.
  • Note that the “<T>” in the class declaration corresponds to its(class) type. To create a pair class comprising string values, specify “String” instead of “Integer”.
  • In the class definition, specify the stated variables followed by the type.
  • In the next step, define a function named “setValue()” having the stated parameters pointing to the passed “key-value” pairs.
  • In its definition, refer to the specified variables and assign them the passed arguments via “this”.
  • After that, define a function named “getValue()” and return the set “key-value” pairs.
  • In the “main()” method, create a class object named “object” via the “new” keyword and the “Template()” constructor, respectively. Also, specify its type, i.e., “Integer”.
  • Now, pass the stated integers in the form of key-value pairs to the invoked set function “setValue()”.
  • Lastly, create another class object via the discussed approach and invoke the “getValue()” function to retrieve the set values in the form of key-value pairs.
  • Note: The getter function can also be invoked by the same object utilized for setting the values.

Output

In the above output, it can be observed that the “key-value” pair is set and retrieved appropriately.

Example 2: Creating a “Pair Class” of Both the Integer and String Types
In this particular example, a pair class of the “Object” type can be created that comprises the “key-value” pair in both the integer and string types:

class Template<T> {
 T val1, val2;
 void setValue(T x, T y){
  this.val1 = x;
  this.val2 = y;
}
 Template getValue(){
  return this;
}}
class pairclass{
 public static void main(String args[]){
  Template<Object> object = new Template<Object>();
  System.out.println("The key-value pair is: ");
  object.setValue(1,"David");
  Template <Object> result= new Template <Object> ();
  result = object.getValue();
  System.out.println(result.val1 + " " + result.val2);
}}

In the above code snippet:

  • Repeat the discussed approaches for creating a class, setting, and getting the passed values, respectively.
  • In the “main()” method, similarly, create two different class objects of the “Object” type and set the “key-value” pairs comprising both the “Integer” and “String” types.
  • Finally, retrieve the set values and display them on the console.

Output

This outcome implies that the fetched “key-value” pairs comprise both the “Integer” and “String” data types.

Conclusion

A “pair class” in Java can be created by setting the key-value pair via the class object and retrieving it with the help of the getter method. These pairs can comprise “Integer”, “String”, or “Object” types. This blog is guided to creating a pair class in Java.

Share Button

Source: linuxhint.com

Leave a Reply