How to create a println/print method for a custom class

You need to override toString():

public class Pair
{
    KeyObject key;
    ValueObject value;

    public Pair(KeyObject k, ValueObject v)
    {
        key = k;
        vale = v;
    }

    // ...
    
    @Override
    public String toString()
    {
         return "Key: " + key.getKey() + " - Value: " + value.getValue();
    }
}

You will need to override the toString method and return a string representation of what you want.

So for example:

public class Pair {

    Object key;
    Object value;

    public Pair(Object k, Object v)
    {
        key = k;
        value = v;
    }

    public Object getKey() { 
        return key; 
    }

    public Object getValue() { 
        return value; 
    }

    public String toString() {
        return "Key: " + getKey() + ", Value: " + getValue();
    }
}

Than you can do the following:

List<Pair> pairs = new ArrayList<Pair>();
pairs.Add(new Pair("pair1key", "pair1value"));
pairs.Add(new Pair("pair2key", "pair2value"));

for (Pair p : pairs) {
    System.out.println(p);
}