BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

How to override toString method for ArrayList in Java

By Chaitanya Singh | Filed Under: java

When we are dealing with ArrayList of Objects then it is must to Override the toString() method in order to get the output in desired format. In this tutorial we will see how to override the toString() method for ArrayList in Java.

Example:

We have two classes here “Student” and “Demo”. Student class has only two properties student name and student age. As you can see that we have overridden the toString() method in Student class itself. In the Demo class we are storing the Student Object in ArrayList and then we iterated the ArrayList using advance for loop. You can very well see that the output is in the format we have specified in toString(). You can give the toString() coding as per your requirement.

package beginnersbook.com;
public class Student 
{
    private String studentname;
    private int studentage;
    Student(String name, int age)
    {
         this.studentname=name;
         this.studentage=age;
    }
    @Override
    public String toString() {
       return "Name is: "+this.studentname+" & Age is: "+this.studentage;
    }
}

Another class:

package beginnersbook.com;
import java.util.ArrayList;
public class Demo
{
     public static void main(String [] args)
     {
          ArrayList<Student> al= new ArrayList<Student>();
          al.add(new Student("Chaitanya", 26));
          al.add(new Student("Ajeet", 25));
          al.add(new Student("Steve", 55));
          al.add(new Student("Mary", 18));
          al.add(new Student("Dawn", 22));
          for (Student tmp: al){
              System.out.println(tmp);
          }
     }
}

Output:

Name is: Chaitanya & Age is: 26
Name is: Ajeet & Age is: 25
Name is: Steve & Age is: 55
Name is: Mary & Age is: 18
Name is: Dawn & Age is: 22

If we wouldn’t have overridden the toString() we would have got the output in below format:
Output of the above programs without overriding toString():

[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

Comments

  1. Noso says

    July 11, 2015 at 5:53 AM

    Simple to read each and every concept

    Reply
  2. Peter says

    January 6, 2017 at 12:10 PM

    Hi,
    I have similar program with same toString method, but my output is with brackets {} and commas.
    Ho can I get rid of brackets and commas in the output?
    I know it should be overriden by some code like >
    String asString = list.toString().replaceAll(“^\\[“, “”).replaceAll(“\\]$”, “”).replace(“,”, “”);
    but don’t know how to implement it.
    Thanks,

    Peter

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap