Open In App

How to Override toString Method for ArrayList in Java?

Last Updated : 04 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Every class in java is a child of Object class either directly or indirectly. toString() is present in Object class. The toString method returns a string representation of an object. The toString() can be overridden as part of a class to cater to the customized needs of the user. Whenever we try to print the Object reference then internally toString() method is invoked. If we did not define the toString() method in your class then the Object class toString() method is invoked otherwise our implemented/Overridden toString() method will be called.

Syntax of Object class toString() Method:

public String toString()
{
    return getClass().getName()+"@"+Integer.toHexString(hashCode());
}

Returns: The method returns a string.

Example:

Java




// Java program to demonstrate
// how to override toString 
// method for ArrayList
  
import java.util.ArrayList;
  
// define a class
class Employee {
  
    // attributes of an Employee
    private String EmployeeName;
    private int EmployeeId;
    private double EmployeeSalary;
  
    // Create Constructor that accept
    // name id and salary as
    // an argument
    Employee(String name, int id, double salary)
    {
        this.EmployeeSalary = salary;
        this.EmployeeName = name;
        this.EmployeeId = id;
    }
  
    // Override toString()
    // provide your own implementation
    public String toString()
    {
        return "   Employee Name = " + this.EmployeeName
            + "    Employee Id = " + this.EmployeeId
            + "    Employee Salary = "
            + this.EmployeeSalary;
    }
}
  
public class GFG {
    public static void main(String[] args)
    {
        // Create a ArrayList of Employee Class Type
        ArrayList<Employee> ArrList
            = new ArrayList<Employee>();
        ArrList.add(new Employee("Mukul", 1001, 52000.0));
        ArrList.add(new Employee("Robin", 1002, 65000.0));
        ArrList.add(new Employee("Rahul", 1003, 53000.0));
        ArrList.add(new Employee("Suraj", 1004, 45000.0));
        ArrList.add(new Employee("Akash", 1005, 38000.0));
  
        // When an object is tried to print
        // toString() method is called
        for (Employee t : ArrList) {
            System.out.println(t);
        }
    }
}


Output

   Employee Name = Mukul    Employee Id = 1001    Employee Salary = 52000.0
   Employee Name = Robin    Employee Id = 1002    Employee Salary = 65000.0
   Employee Name = Rahul    Employee Id = 1003    Employee Salary = 53000.0
   Employee Name = Suraj    Employee Id = 1004    Employee Salary = 45000.0
   Employee Name = Akash    Employee Id = 1005    Employee Salary = 38000.0

Explanation: When we try to print Employee instance, toString() method which is overridden is called and the string value is printed.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads