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
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()); } }
Count of Elements in the AbstractCollection = 6 AbstractCollection = [HDD, Earphone, Headphone, Card Reader, SSD, Pen Drive]