Sunday, February 8, 2026
HomeLanguagesJavaCreating a User-Defined Printable Pair Class in Java

Creating a User-Defined Printable Pair Class in Java

The pair class in C++ Standard Library is used a lot.  We can implement our own user-defined pair class in Java and its object can be used anywhere just like any other parameter.

Note : 

This class is equivalent to pair<int,int> class in java. You can create your own template or classes for other data types.

Syntax For defining the pair class can be:

class pair{
   int first,second;
   
   // constructor for assigning values
   pair(int first,int second){
       this.first = first;
       this.second = second;
   }
   
   // function which returns a
   pair values(){
       return new pair(first,second);
   }
   
   // printing the pair class
   @Override
   public String toString(){
       return "("+first+","+second+")";
   }
}
  • We can now play around with this class and even create an array of pairs, similar to the vector<pair<int,int>> in C++.

Example:

Java




// Java program to create a pair class
// and initialize an array of that
// pair class object
 
public class pair_example {
    public static void main(String[] args)
    {
        pair[] arr = new pair[5];
        for (int i = 0; i < 5; i++) {
            arr[i] = new pair(i + 1, i + 2);
        }
       
        // printing an array of pairs easily
        for (pair i : arr) {
            System.out.println(i);
        }
       
        // to extract particular values
        int value = arr[3].second;
        System.out.println("Required Value = " + value);
    }
}
 
// user defined pair class of integer type
class pair {
    int first, second;
   
    // constructor for assigning values
    pair(int first, int second)
    {
        this.first = first;
        this.second = second;
    }
   
    // function which returns a pair
    pair values() { return new pair(first, second); }
   
    // printing the pair class
    @Override public String toString()
    {
        return first + "," + second;
    }
}


 
 

Output

1,2
2,3
3,4
4,5
5,6
Required Value = 5

 

Implementation Example:

 

Suppose we need to store the ith prime number and its index together in a structure.

 

Java




// Java program to create a struct/pair class
// for storing a prime number with its index
 
class GFG {
    public static void main(String[] args)
    {
        // first N prime numbers
        int N = 30;
 
        // creating list of pairs
        java.util.ArrayList<pair> p = new java.util.ArrayList<>();
       
        int index = 1;
        for (int i = 1; i <= N; i++) {
            if (isPrime(i)) {
               
                // creating new pair object and appending to
                // list
                p.add(new pair(index++, i));
            }
        }
       
        System.out.println("i and the ith prime numbers are :");
        System.out.println(p);
    }
   
    // function to check prime
    static boolean isPrime(int n)
    {
        if (n < 2)
            return false;
        for (int i = 2; i * i <= n; i++) {
            if (n % i == 0)
                return false;
        }
        return true;
    }
}
 
// user defined pair class of integer type
class pair {
    int first, second;
 
    // constructor for assigning values
    pair(int first, int second)
    {
        this.first = first;
        this.second = second;
    }
   
    // function which returns a pair
    pair values() { return new pair(first, second); }
   
    // printing the pair class
    @Override public String toString()
    {
        return "(" + first + "," + second + ")";
    }
}


 
 

Output

i and the ith prime numbers are :
[(1,2), (2,3), (3,5), (4,7), (5,11), (6,13), (7,17), (8,19), (9,23), (10,29)]

 

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32493 POSTS0 COMMENTS
Milvus
126 POSTS0 COMMENTS
Nango Kala
6864 POSTS0 COMMENTS
Nicole Veronica
11990 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12084 POSTS0 COMMENTS
Shaida Kate Naidoo
7000 POSTS0 COMMENTS
Ted Musemwa
7241 POSTS0 COMMENTS
Thapelo Manthata
6951 POSTS0 COMMENTS
Umr Jansen
6936 POSTS0 COMMENTS