beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

How to override toString method for ArrayList in Java

By Chaitanya Singh | Filed Under: Java Collections

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]

Enjoyed this post? Try these related posts

  1. Java – Search elements in LinkedList example
  2. Java ArrayList set() Method example
  3. How to copy and add all list elements to ArrayList in Java
  4. Remove Key-value mapping from TreeMap example
  5. Vector ListIterator example in Java
  6. Java – Add elements at beginning and end of LinkedList example

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 Tutorial
  • OOPs Concepts

Java Collections

  • ArrayList
  • LinkedList
  • ArrayList vs LinkedList
  • Vector
  • ArrayList vs Vector
  • HashMap
  • TreeMap
  • LinkedHashMap
  • HashSet
  • TreeSet
  • LinkedHashSet
  • Hashtable
  • HashMap vs Hashtable
  • Queue
  • PriorityQueue
  • Deque & ArrayDeque
  • Iterator
  • ListIterator
  • Comparable Interface
  • Comparator Interface
  • Java Collections Interview Q

MORE ...

  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap