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

I work in Java on a project that requires me to make some โ€œcontainerโ€ classes if you want. Here is a simple version:

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; } } 

(Note that this is greatly simplified, and I use the correct set / get methods in the final version.)

My question is:

When calling the println method with the ArrayList parameter as a parameter, for example:

 ArrayList<String> arr = new ArrayList<String>(); arr.add("one"); arr.add("two"); System.out.println(arr); 

Java automatically knows how to correctly print each "node" or ArrayList element.

Is there a way to write a method that allows the println rule to correctly print my Pair class?

+6
source share
6 answers

You 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(); } } 

How can you 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); } 
+13
source

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() + " - Vlaue: " value.getValue(); } } 
+5
source

You will need to define the toString() method. It is automatically called System.out.println(Object o) . The ArrayList class has such an overridden toString() method that provides a well-formatted representation.

Basically, overriding Object.toString with your own definition is all that is required.

+1
source

You can override the toString() method of your custom class and print any necessary information.

 @Override public String toString() { return .....; } 
+1
source

You can extend the ArrayList and override the toString() method:

 public class MyArrayList<T> extends ArrayList<T> { @Override public String toString() { // format your print here... } } 

But that is too much. I would just write a print utility method.

 public class MyUtils { public String toString( ArrayList<? extends Object> ) { // format your print here; } } 
+1
source
 /** demo print of an ArrayList of Address objects using overridden toString() method */ import java.util.ArrayList; public class Address { // simple address fields (incomplete) private String addrLine1; private String city; private String state; // run sample program public static void main(String[] args) { new Address().run(); } public void run() { // instantiate example Address records Address addr1 = new Address(); Address addr2 = new Address(); // add example field data addr1.addrLine1 = "123 This St."; addr1.city = "Big Tuna"; addr1.state = "Texas"; addr2.addrLine1 = "456 That St."; addr2.city = "Phoenix"; addr2.state = "Arizona"; // create ArrayList<Address>, add instances ArrayList<Address> addrArray = new ArrayList<>(); addrArray.add(addr1); addrArray.add(addr2); // print Address instance fields in addrArray for ( Address addr : addrArray ) System.out.println(addr); } // end run() // overriding toString() method @Override public String toString() return "\n" + addrLine1 + "\n" + city + ", " + state; } // end class Address 

Print

123 This St. Petersburg Big Tuna, Texas

456 What is St. Phoenix, Arizona

0
source

Source: https://habr.com/ru/post/900732/


All Articles