The toString() method of Java AbstractCollection class

Java 8Object Oriented ProgrammingProgramming

The toString() method of the AbstractCollection class is used to return the string representation of the elements of this collection.

The syntax is as follows

public String toString()

To work with AbstractCollection class in Java, import the following package

import java.util.AbstractCollection;

The following is an example to implement AbstractCollection toString() method in Java

Example

 Live Demo

import java.util.ArrayList;
import java.util.AbstractCollection;
public class Demo {
   public static void main(String[] args) {
      AbstractCollection<Object> absCollection = new ArrayList<Object>();
      absCollection.add("HDD");
      absCollection.add("Earphone");
      absCollection.add("Headphone");
      absCollection.add("Card Reader");
      absCollection.add("SSD");
      absCollection.add("Pen Drive");
      System.out.println("Count of Elements in the AbstractCollection = " + absCollection.size());
      System.out.println("AbstractCollection = " + absCollection.toString());
   }
}

Output

Count of Elements in the AbstractCollection = 6
AbstractCollection = [HDD, Earphone, Headphone, Card Reader, SSD, Pen Drive]
raja
Published on 28-Mar-2019 11:39:20
Advertisements